0

Ok guys, I have searched for an answer and I could not find anything to help. I tried using a while loop and a forloop but only one file ever gets uploaded. Here is my code.

Form:

<form method="post" enctype="multipart/form-data" action="process.php">
<div id="filediv">
<div id="imagefiles">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label>Upload File:
<input name="userfile[]" type="file" id="userfile" multiple></label>
<label>Alt Text: <input name="alt" type="text"></label>
 </div>
 </div>

Here is the upload function:

$alt=mysqli_real_escape_string($conn, $_POST['alt']);
foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){
if (($_FILES["userfile"]["error"] == 0) && ($_FILES['userfile']['size'] > 0))
{
$fileName = $_FILES['userfile']['name'][$key];
$tmpName  = $_FILES['userfile']['tmp_name'][$key];
$fileSize = $_FILES['userfile']['size'][$key];
$fileType = $_FILES['userfile']['type'][$key];
} else{
    echo"error";
}

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["userfiles"]["name"]));
if((($_FILES["userfile"]["type"] == "image/gif")
    ||($_FILES["userfile"]["type"]=="image/jpeg")
    ||($_FILES["userfile"]["type"]=="image/png")
    ||($_FILES["userfile"]["type"]=="image/pjpeg")
    && in_array($extension, $allowedExts)))
    {
        $fp = fopen($tmpName, 'r');
        $content =fread($fp, filesize($tmpName));
        $SourceImage = imagecreatefromstring($content);
        $SourceWidth = imagesx($SourceImage);
        $SourceHeight=imagesy($SourceImage);
        $DestWidth=100;
        $DestHeight=130;
        if ($SourceHeight> $SourceWidth)
        {$ratio = $DestHeight / $SourceHeight;
        $newHeight = $DestHeight;
        $newWidth = $sourceWidth * $ratio;
        }
        else
        {
            $ratio = $DestWidth / $SourceWidth;
            $newWidth = $DestWidth;
            $newHeight = $SourceHeight * $ratio;
        }
        $DestinationImage = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($DestinationImage, $SourceImage, 0,0,0,0,$DestWidth, $DestHeight, $SourceHeight, $SourceWidth);
        ob_start();
        imagejpeg($DestinationImage);
        $BinaryThumbnail = ob_get_contents();
        ob_end_clean();
        $thumb = addslashes($BinaryThumbnail);
        $content = addslashes($content);
        fclose($fp);
        $fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

         mysqli_query($conn, "INSERT INTO files (username, name, size, content, type, link, alt, thumbnail) VALUES ('$username', '$fileName', '$fileSize', '$content', '$fileType', 1, '$alt', '$thumb')") or die('Error, query failed'); 
           echo "<script>alert('The file has been uploaded');location.replace('uploaded.php');</script>";
           unlink ($_FILES['username']['tmp_name']);
    }else{ 
           echo "<script>alert('Please upload an image');location.replace('upload.php');</script>";
    }

}
}

I realized that I didn't need half the code that I had. Now I have one image uploading but not both again.

Pureblood
  • 125
  • 1
  • 3
  • 9
  • Is this related or helpful? http://stackoverflow.com/questions/1361673/get-raw-post-data – jdog Oct 28 '13 at 20:17
  • I don't think that is it. The first file does upload, but it doesn't do more than one. My loops I have tried it doesn't work. – Pureblood Oct 28 '13 at 20:28
  • when I try your html, $_FILES is completely empty. I'm not sure if PHP supports the multiple attribute, hence I suggested looking at RAW HTTP data – jdog Oct 28 '13 at 20:30
  • I'm still haveing trouble getting the multiple files to upload. I added the foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){ but it didn't work. – Pureblood Oct 29 '13 at 02:47

2 Answers2

0

I might be wrong, I'm not sure I ever coded a $_FILE array....but I think the issue is here

 while ($_FILES["userfile'"]["tmp_name"][$counter]){

besides the extra quote I don't think the data is stored like that...you can try doing print_r($_FILES)

It might be

$_FILES["userfile"][$counter]["tmp_name"]

but I'm actually doubting if this will even work. Just loop through the $_FILE array itself to handle an dynamic number of files....here is the function I use....

public static function upload($file, $accountDirectory, $uploadDirectory)
{
    $return = array();
    if(!file_exists($file['tmp_name']) || !is_uploaded_file($file['tmp_name'])) {
        $return["result"] = false;
    }
    if($file['error'] != UPLOAD_ERR_OK) {
         $return["result"] = false; 
          switch($file['error']){
            case 0: //no error; possible file attack!
              echo "There was a problem with your upload.";
              break;
            case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
              echo "The file you are trying to upload is too big.";
              break;
            case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
              echo "The file you are trying to upload is too big.";
              break;
            case 3: //uploaded file was only partially uploaded
              echo "The file you are trying upload was only partially uploaded.";
              break;
            case 4: //no file was uploaded
              echo "You must select an image for upload.";
              break;
            default: //a default error, just in case!  :)
              echo "There was a problem with your upload.";
              break;            
          }         
    } else {
        $newFileName = preg_replace("/[^a-zA-Z0-9-.]/", "", $file["name"]);
        $uploadLocation = $accountDirectory . $uploadDirectory . $newFileName;
        while (file_exists($uploadLocation)) {
            $uploadLocation = $accountDirectory . $uploadDirectory . time() . $newFileName;
        }
        if (move_uploaded_file($file["tmp_name"],$uploadLocation)==true) {
            $return["file"] = str_replace($accountDirectory, "/",$uploadLocation);
            $return["result"] = true;
        } else {
            $return["result"] = false;
        }
    }
    return $return;
}
hendr1x
  • 1,470
  • 1
  • 14
  • 23
0

By doing $_FILES['userfile'] and $_FILES['file'], you are referring only to files uploaded via upload fields with those names (userfile and file, respectively). $_FILES is an associative array, so you should do something like

foreach ($_FILES as $fieldName => $fileProperties) {
    // do something
}

Also, note that you have $_FILES["userfile'"] about a dozen lines down; the extra ' will break that line.

See the PHP help files for more info.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Thank you. You made me realize that I didn't even need a section of my code. – Pureblood Oct 28 '13 at 21:42
  • I'm still haveing trouble getting the multiple files to upload. I added the foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){ but it didn't work. – Pureblood Oct 29 '13 at 02:48
  • I think you need to use `foreach($_FILES['userfile'] as $key => $values ){`, not `foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){`. `$_FILES['userfile']` should be an array of files, each of which has its own `tmp_name` key. You might try doing `var_dump($_FILES);` to see precisely what your array looks like; it could be very helpful in spotting the issue. (Note: I recommend putting `echo '
    ';` before the vardump and `echo '
    ';` after it, so it's easier to read.)
    – elixenide Oct 29 '13 at 02:56