0

I have a PHP image upload code and it doesn't show any error or warning after running code but it doesn't upload image to folder or does nothing

php

    <?php

if (isset($_FILES['files'])) {
    $uploadedFiles = array();
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
        $errors = array();
        $rand_name = microtime().microtime().time().microtime();
        echo $rand_name;
        $file_name = microtime().time();
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];

        if($file_type == "image/gif"){
            $sExt = ".gif";
        } elseif($file_type == "image/jpeg" || $file_type == "image/pjpeg"){
            $sExt = ".jpg";
        } elseif($file_type == "image/png" || $file_type == "image/x-png"){
            $sExt = ".png";
        }
        if (!in_array($sExt, array('.gif','.jpg','.png'))) {
            $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
        }
        if ($file_size > 2097152000) {
            $errors[] = 'File size must be less than 2 MB';
        }
        $desired_dir = "user_data/";
        if (empty($errors)) {
            if (is_dir($desired_dir) == false) {
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if (move_uploaded_file($file_tmp, "$desired_dir/" . $file_name . $sExt)) {
                $uploadedFiles[$key] = array($file_name . $sExt, 1);
            } else {
                echo "Couldn't upload file " . $_FILES['files']['name'][$key];
                $uploadedFiles[$key] = array($_FILES['files']['name'][$key], 0);
            }
        } else {
            print_r($errors);
        }
    }

    foreach ($uploadedFiles as $key => $row) {
        if (!empty($row[1])) {
            $codestr = '$file' . ($key+1) . ' = $row[0];';
            eval ($codestr);
        } else {
            $codestr = '$file' . ($key+1) . ' = NULL;';
            eval ($codestr);
        }
    }
}

HTML

<form action="for.php" method="post" enctype="multipart/form-data">
    Attachment(s): <input type="file" name="files[]" multiple id="files" class="hidde fileInpu"/>
    <input type="submit" name="submit" value="Request">

I ran this code in NetBeans with WampServer but didn't encounter any error.

1 Answers1

0

This line raises questions for me:

$file_name = md5(uniqid("") . time());

Is this a function or a typo?

At no point do you check for file upload errors using $_FILES['files']['error'] , try getting some feedback from that error array.

Also $file_size > 2097152000 is far too large, as a 2Mb file is infact 2097152, see Don't allow > 2mb images .

If you are in XHTML your multiple needs to be multiple = "multiple" , you look like XHTML as your HTML element ending is />

You need to check your PHP Error log and see what's turned up. Please see Where does PHP store the error log? (php5, apache, fastcgi, cpanel) for more information, if you're not sure what/where the error log is.

I'm pretty sure one of these points will solve your issue.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
  • i cant upload multiple images –  Mar 01 '15 at 14:48
  • hmm, that's against all the code in your example, which is geared towards multiple images in the foreach loops. can you share which issue was the cause of your original error? – Martin Mar 01 '15 at 18:02
  • when i upload multiple images total size more than 2MB images are not upload so i made increase in `$file_size > 8388608` even after making change coudn't upload –  Mar 01 '15 at 18:16