SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    move_uploaded_file

    hey, i am having some problems with this function [move_uploaded_file]. it works, but when i try open the file it moved, the file has no contents (the file size is correct though). everything works with copy(), but i would like to use move_uploaded_file (i don't know why, just cuz php recommends it)

    my code looks sumpin like this:

    PHP Code:
    for($i=0$i<$count$i++) { move_uploaded_file($userfile['tmp_name'][$i], $dir.$userfile['name'][$i])

    something like that... help!

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    could you post more code please ?

    I find this bit wierd, sure the [$i] is after the ['tmp_name'] and ['name'] ? not before ?

    if the file size is correct though this sounds very wierd, more code would be appreciated...
    - website

  3. #3
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    some more code

    i give users the option as to how much file upload fields they want to display, these first two actions just clean that information up. hope this is more clear, this is my exact code.

    PHP Code:
    //get amount of form fields chosen
    $count count($_FILES['userfile']['name']);

    //clean up FILES array
    for( $i=0$i<$count$i++ ) {
      if( 
    $_FILES['userfile']['error'][$i] != ){
      unset(
    $_FILES['userfile']['name'][$i]);
      unset(
    $_FILES['userfile']['type'][$i]);
      unset(
    $_FILES['userfile']['tmp_name'][$i]);
      unset(
    $_FILES['userfile']['error'][$i]);
      unset(
    $_FILES['userfile']['size'][$i]);
      }
    }

    //re-value the file name array
    $name_array array_values($_FILES['userfile']['name']);
    $type_array array_values($_FILES['userfile']['type']);
    $tmp_array array_values($_FILES['userfile']['tmp_name']);
    $error_array array_values($_FILES['userfile']['error']);
    $size_array array_values($_FILES['userfile']['size']);

    //make monster array
    $monster = array("name" => $name_array"type" => $type_array"tmp_name" => $tmp_array"error" => $error_array"size" => $size_array);

    //update count
    $count count($monster['name']);

    //make dir
    mkdir($uploaddir."order".$order_id0777);
    $fdir $uploaddir."order".$order_id."/";
    chmod$fdir0777 );

    //move files
    for( $i=0$i<$count$i++ ) {
    if (
    move_uploaded_file($monster['tmp_name'][$i], $fdir $monster['name'][$i])) {
        print 
    "File ".$monster['name'][$i]." is valid, and was successfully uploaded.<br>\n";
    }
    else {
    print 
    "File did not upload";


  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    noooo I dont think that would work.... how is the form? and did that code upload all files if you changed move_uploaded_file to copy ?

    What I think you need is first to ask the user how many files they want to upload, then you print out a form with <input type="file" name="userfile[]"
    and then do foreach file that is uploaded

    at least that is how I would try to do it
    hope that helps
    - website

  5. #5
    SitePoint Zealot poLka's Avatar
    Join Date
    Apr 2003
    Location
    GF
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's exactly what is happening... on the previous page <input type="file" name="userfile[]"> this works, but when i open the file it is empty, although the file size is the same. if i switch to the copy() function, all is good. i would just like to use the PHP recommended function move_uploaded_file().

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here's how I upload photos one-at-a-time ... changing it to loop through an array of files should be easy ...
    PHP Code:
    $errormsg ""// A variable to hold error messages

    // MAX_FILE_SIZE is a defined constant determining the max number of bytes allowed

    // UPLOAD_PATH is a defined constant determining the physical path to the store folder

    $filename $_FILES['upload_file']['name'];
    $filetemp $_FILES['upload_file']['tmp_name'];
    $filetype $_FILES['upload_file']['type'];
    $fileext strtolower (substr ($filenamestrlen ($filename) - 4));
    $filesize = (int) $_FILES['upload_file']['size'];

    if ( 
    $fileext == ".jpg" || $fileext == ".png" && strlen ($errormsg) < 5) {

        if (
    $filesize MAX_FILE_SIZE && $filesize 0) {
                
            
    // Has file upload gone OK?
            
    if (is_uploaded_file ($filetemp)) {

                
    // Get the width and height of the image
                
    $size = @getimagesize ($filetemp);
                
    $width $size[0];
                
    $height $size[1];

                
    // Make a unique filename
                
    $filename $uniqueid $fileext;

                
    // Store the file on the server
                
    if (!@move_uploaded_file ($filetempUPLOAD_PATH.$filename)) {
                    
    $errormsg .= " Problem uploading the file (storing on server). Please try again. If the problem persists please email <a href=\"mailto:$contactemail\">$contactemail</a> to report the error.";
                } else {
                    @
    chmod (UPLOAD_PATH.$filename0644);
                }

            } else {
                
    $errormsg .= " Possible file upload attack, or the file size exceeds the maximum allowed by PHP. Filename: "$filename ."; Filesize: "$filesize;
            } 
    // end if is_uploaded
                
        
    } else {
            
    $errormsg .= " Invalid file size. Maximum of "MAX_FILE_SIZE ." bytes allowed. (Your file is $filesize bytes)";
        } 
    // end if filesize
    } else {
        
    $errormsg .= " Invalid file type. Only photos (.jpg and .png) files allowed";
    // end if correct file extension 
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    polka, looked really hard at your script and I don't see anything wrong...
    Are you sure your php verision supports move_uploded_file() ?

    This is very wierd though... only thing I can think of around this is to change this:
    PHP Code:
    for( $i=0$i<$count$i++ ) { 
      if( 
    $_FILES['userfile']['error'][$i] != ){ 
      unset(
    $_FILES['userfile']['name'][$i]); 
      unset(
    $_FILES['userfile']['type'][$i]); 
      unset(
    $_FILES['userfile']['tmp_name'][$i]); 
      unset(
    $_FILES['userfile']['error'][$i]); 
      unset(
    $_FILES['userfile']['size'][$i]); 
      } 

    to
    PHP Code:
    for( $i=0$i<$count$i++ ) { 
      if( 
    $_FILES['userfile']['error'][$i] != || !is_uploded_file($_FILES['userfile']['tmp_name'][$i])){ 
      unset(
    $_FILES['userfile']['name'][$i]); 
      unset(
    $_FILES['userfile']['type'][$i]); 
      unset(
    $_FILES['userfile']['tmp_name'][$i]); 
      unset(
    $_FILES['userfile']['error'][$i]); 
      unset(
    $_FILES['userfile']['size'][$i]); 
      } 

    this would make sure that the file is uploaded via post method...
    very wierd I must say, please reply if you find solution!

    and siteguru:
    why do is_uploaded_file() and then move_uploaded_file() ?move_uploaded_file() does the same as is_uploaded_file() + copy(), just no point in double checking it

    my point is... if you use is_uploaded_file() and then copy() it is just as safe as doing just move_uploaded_file()

    hope that helps...
    - website


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
  •