Results 1 to 3 of 3

Thread: PHP File Upload Help

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default PHP File Upload Help

    Ok I have a an upload form which works, but I don't have a filter. The user can uploaded any files... I don't want someone uploading viruses or stuff that isn't allowed on...
    I have both the HTML page and the php page
    HTML Page:
    HTML Code:
    <html>
    <body>
    <form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    </form>
    </body>
    </html>
    PHP page:
    PHP Code:
    <?php
    $target_path 
    "uploads/";
    $target_path $target_path basename$_FILES['uploadedfile']['name']); 

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo 
    "The file ".  basename$_FILES['uploadedfile']['name']). 
        
    " has been uploaded";
    } else{
        echo 
    "There was an error uploading the file, please try again, either rename your file or !";
    }
    ?>
    BTW I did not write the code above i got that from a site...

    (Just wondering: if there is a way to check to see if that file already exists is there a way to either rename the file or to tell the user to rename the file... thanks!)

    Edit:
    I only want these types of files being uploaded:
    PNG, JPG, GIF, MOV, MPG, AVI, MP3
    Thanks

  2. #2
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    Oh I found one... it was off PHP Freaks.com sorry guys...
    heres the code...
    Code:
    <?php
    $upload_dir = "submitions/";
    
    $num_files = 5;
    //the file size in bytes.
    $size_bytes =200000; //51200 bytes = 50KB.
    //Extensions you want files uploaded limited to.
    $limitedext = array(".gif",".jpg",".jpeg",".png",".bmp",".mov",".mpg",".mpeg");
    
    
       //check if the directory exists or not.
       if (!is_dir("$upload_dir")) {
          die ("Error: The directory <b>($upload_dir)</b> doesn't exist because we are in the process of the weekly upload.");
       }
       //check if the directory is writable.
       if (!is_writeable("$upload_dir")){
          die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
       }
    
    
    //if the form has been submitted, then do the upload process
    //infact, if you clicked on (Upload Now!) button.
    if (isset($_POST['upload_form'])){
    
           echo "<h3>Upload results:</h3>";
    
           //do a loop for uploading files based on ($num_files) number of files.
           for ($i = 1; $i <= $num_files; $i++) {
    
               //define variables to hold the values.
               $new_file = $_FILES['file'.$i];
               $file_name = $new_file['name'];
               //to remove spaces from file name we have to replace it with "_".
               $file_name = str_replace(' ', '_', $file_name);
               $file_tmp = $new_file['tmp_name'];
               $file_size = $new_file['size'];
    
               #-----------------------------------------------------------#
               # this code will check if the files was selected or not.    #
               #-----------------------------------------------------------#
    
               if (!is_uploaded_file($file_tmp)) {
                  //print error message and file number.
                  echo "File $i: Not selected.<br>";
               }else{
                     #-----------------------------------------------------------#
                     # this code will check file extension                       #
                     #-----------------------------------------------------------#
    
                     $ext = strrchr($file_name,'.');
                     if (!in_array(strtolower($ext),$limitedext)) {
                        echo "File $i: ($file_name) Wrong file extension. <br>";
                     }else{
                           #-----------------------------------------------------------#
                           # this code will check file size is correct                 #
                           #-----------------------------------------------------------#
    
                           if ($file_size > $size_bytes){
                               echo "File $i: ($file_name) Faild to upload. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
                           }else{
                                 #-----------------------------------------------------------#
                                 # this code check if file is Already EXISTS.                #
                                 #-----------------------------------------------------------#
    
                                 if(file_exists($upload_dir.$file_name)){
                                     echo "File $i: ($file_name) already exists.<br>";
                                 }else{
                                       #-----------------------------------------------------------#
                                       # this function will upload the files.  :) ;) cool          #
                                       #-----------------------------------------------------------#
                                       if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
                                           echo "File $i: ($file_name) Uploaded.<br>";
                                       }else{
                                            echo "File $i: Faild to upload.<br>";
                                       }#end of (move_uploaded_file).
    
                                 }#end of (file_exists).
    
                           }#end of (file_size).
    
                     }#end of (limitedext).
    
               }#end of (!is_uploaded_file).
    
           }#end of (for loop).
           # print back button.
           echo "&#187;<a href=\"$_SERVER[PHP_SELF]\">back</a>";
    ////////////////////////////////////////////////////////////////////////////////
    //else if the form didn't submitted then show it.
    }else{
        echo " <h3>Select files to upload!.</h3>
               Max file size = ". $size_bytes / 1024 ." KB";
        echo " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\" enctype=\"multipart/form-data\">";
               // show the file input field based on($num_files).
               for ($i = 1; $i <= $num_files; $i++) {
                   echo "File $i: <input type=\"file\" name=\"file". $i ."\"><br>";
               }
        echo " <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$size_bytes\">
               <input type=\"submit\" name=\"upload_form\" value=\"Upload Now!\">
               </form>";
    }
    ?>

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Thanks! I was working on an upload script that needed filters. Thanks for the post.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •