SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File upload script

    Hi,

    The following code echoes that the file(s) were uploaded with no errors but when I check the directory no files exist, If I alter the file upload path to something that doesnt exist I get errors as I should.

    Any ideas why its not actually working when it comes back with a successful result?

    PHP Code:
    <?php  

      $numoffile 
    1;  

      
    $file_dir  "delete";  
      if (
    $_POST) {  
        for (
    $i=0;$i<$numoffile;$i++) {  
          if (
    trim($_FILES['myfiles']['name'][$i])!="") {  
            
    $newfile $file_dir.$_FILES['myfiles']['name'][$i];  
            
    move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);  
            
    $j++;  
          }  
        }  
      }  
      
        if (isset(
    $j)&&$j>0) { echo 'Your file(s) has been uploaded.<br />';
        }else{  
          echo 
    '<form method="post" enctype="multipart/form-data">';  
        for(
    $i=0;$i<$numoffile;$i++) {  
        echo 
    '<input type="file" name="myfiles[]" size="30"><br />';  
        }  
          echo 
    '<input type="submit" name="action" value="Upload">';  
          echo 
    '</form>';
        }
    ?>

    Also how can limit the file type to *.zip and *.rar files and also the maximum file size?


    Thanks

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
    Arn't you missing the directory / ?

    $newfile = $file_dir.'/'.$_FILES['myfiles']['name'][$i];

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the variable for $file_dir is set at the top of the code

    $file_dir = "delete";

    ?

    if i change $file_dir = "delete"; to say $file_dir = "whatever"; and there is no folder on the server called whatever I then get a copy error

    but with $file_dir = "delete"; and a folder on the server called delete with chmod 777 I get a succsessfull echo but no files in the directory

    Thanks

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, but if $file_dir is 'delete' and the filename is 'foo' for example then you're trying to move it to deletefoo instead of delete/foo

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmz I thought that

    $file_dir.'/'.$_FILES['myfiles']['name'][$i]; = delete/filename ....

    thats not correct?

  6. #6
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's correct, but you don't have that, in your first post above you have:

    $newfile = $file_dir.$_FILES['myfiles']['name'][$i];

    no slash

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay right you are, was looking at yer posted code and not my own and saw the / in there .. Thanks

    Whats the general concept to lock it down to certain file types

    I know for images its like

    $allowed_photo_types = array(
    'image/jpeg' => 'jpg',
    'image/gif' => 'gif');

    is it

    $allowed_file_types = array(
    'file/zip' => 'zip',
    'file/rar' => 'rar');

    ?

    Thanks

  8. #8
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    application/zip application/rar

  9. #9
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I have tried the following but it wont allow any file types through

    PHP Code:
    $allowed_file_types = array(
    'application/rar' => 'rar',
    'application/zip' => 'zip');


      
      if (
    $_POST) {  
      
          if(!
    array_key_exists($myfiles['type'][$i], $allowed_file_types)) {
        echo 
    'The file type is not accepted for upload';
        }else{ 
    Am I on the right track?


    Thanks

  10. #10
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do a var_dump($myfiles); and see what's in there (presuming you don't mean $_FILES['myfiles'] as that's what you use above).

  11. #11
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still havnt grasped it yet

    PHP Code:
    $allowed_file_types = array(
    'application/rar' => 'rar',
    'application/zip' => 'zip');


      
      if (
    $_POST) {  
          if(!
    array_key_exists($_FILES['myfiles']['type'], $allowed_file_types)) {
        echo 
    'The file type is not accepted for upload';
        }else{
          for (
    $i=0;$i<$numoffile;$i++) {  
          if (
    trim($_FILES['myfiles']['name'][$i])!="") {  
            
    $newfile $file_dir.'/'.$_FILES['myfiles']['name'][$i]; 
            
    move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);  
            
    $j++;  
          } 
    var_dump($_FILES['myfiles']); contains the data while $myfiles is obviously blank, but still get the file type prompt


    Thanks

  12. #12
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you're uploading multiple files $_FILES['myfiles']['type'] will be an array so you need to use it the same way you're using 'name' and 'size' etc, i.e $_FILES['myfiles']['type'][$i]

    So you'll probably want to do the type check inside your existing for ($i=0;$i<$numoffile;$i++) { loop

  13. #13
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still not following it

    PHP Code:
    <?php  

        $numoffile 
    2;  
        
    $file_dir  "delete"
         
    $allowed_file_types = array(
    'application/rar' => 'rar',
    'application/zip' => 'zip');

    if (
    $_POST) {  
        for (
    $i=0;$i<$numoffile;$i++) {  
        if(!
    array_key_exists($_FILES['myfiles']['type'][$i], $allowed_file_types)) {
        echo 
    'File No '.($i+1).' is not an accepted file type for uploading<br />';
        
        }else{
          
    if (
    trim($_FILES['myfiles']['name'][$i])!="") {  
        
    $newfile $file_dir.'/'.$_FILES['myfiles']['name'][$i]; 
        
    move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);  
        
    $j++;  
       }  
      }  
     }

      
    if (isset(
    $j)&&$j>0) { echo 'Your file(s) has been uploaded.<br />';
        }else{  
          echo 
    '<form method="post" enctype="multipart/form-data">';  
        for(
    $i=0;$i<$numoffile;$i++) {  
        echo 
    '<input type="file" name="myfiles[]" size="30"><br />';  
        }  
          echo 
    '<input type="submit" name="action" value="Upload">';  
          echo 
    '</form>';
        }

    ?>
    Thats what have right now, when I upload *.rar files I get the echo of is not an accepted file type ...


    Thanks

  14. #14
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the echo to this and see what it says:

    echo 'File No '.($i+1).' is not an accepted file type for uploading<br />';
    echo 'It is of type: '.$_FILES['myfiles']['type'][$i].'<br />';

  15. #15
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that brings back

    File No 1 is not an accepted file type for uploading
    It is of type: application/x-zip-compressed
    File No 2 is not an accepted file type for uploading
    It is of type: application/x-zip-compressed

  16. #16
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then that's what's should be in the array not application/rar
    The type can vary from server to server depending on the setup, application/rar was just my best guess

  17. #17
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    724
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice, thats sorted it .. Thanks

    Is it possible to upload a number of files via a form then have it zip and store the files as a single zip file?

  18. #18
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Get a copy of the PHP manual and look at the gzencode() and related functions.
    Ian Anderson
    www.siteguru.co.uk


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
  •