0

I am trying to upload multiple images into the folder using php . The code can print out the file names which means I get the files but now it does not upload them and I get no error : below is my code

<?php 
    $target = "image_uploads/";
    if(isset($_FILES['FILE_NAME'])){
        foreach($_FILES['FILE_NAME']['tmp_name']as $key => $error ){
           print_r($key);
           $file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
           #print image names
           echo $file_upload.'</br>';
          move_uploaded_file($file_upload,$target);    
        }
    }
?>
1
  • Hey, if you're sending a multiple file, $_FILES should be an array of files and should be used like this : $_FILES['FILE_NAME'][0]['tmp_name'] for the first one etc. Commented Feb 8, 2013 at 13:02

4 Answers 4

2

In target you have to give the file name too. Please use the code below,

$target = "image_uploads/";
if(isset($_FILES['FILE_NAME'])){
    foreach($_FILES['FILE_NAME']['tmp_name'] as $key => $error ){
        print_r($key);
        $file_upload = $key.$_FILES['FILE_NAME']['name'][$key];
        print image names
        echo $file_upload.'</br>';
        move_uploaded_file($file_upload,$target.$_FILES['FILE_NAME']['name']);    
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@humphrey: whether it is coming inside foreach loop or not? give some space in foreach as
@humphery: name of the file is $_FILES['FILE_NAME']['name']. i have edited. please use that
1

I think the problem is in the foreach loop.

foreach ($_FILES['FILE_NAME']['tmp_name'] as $key => $val) {
    // this loops through the tmp_name of $_FILES['FILE_NAME']
    // which is a string
}

I think you meant something like:

foreach ($_FILES as $index => $fileArray) {
    $tmpName = $fileArray['tmp_name'];
    echo "File at key $index is temporarily uploaded at $tmpName";
}

The code above will loop through all uploaded files and print it's current filename.

3 Comments

thanks but also mine does but the problem is it doe not upload to the folder
have you checked is_writable($target), to check if php can write to the folder?
@ChrisF no but I had another code which could do upload but all i want now to start my own code so that I understand nicely (thanks Chris )
0

It might happen that your target folder is not writable.

I also think, that the cause of which you're not getting errors is, that you have the following:

print_r($key);

Yous should have:

print_r($error);

Comments

0

There can be multiple reasons for this :

  1. The target folder must exist before trying to move the file from temp location to the target and must also be writable

  2. the move_uploaded_file takes the second argument as the file name followed by the directory name, so it can be something like : target folder/user.file.name.ext

  3. If you are uploading multiple files, then the $_FILES must be accessed as shown in the link : http://php.net/manual/en/features.file-upload.multiple.php

  4. for the php error messages that you may encounter, here is a list : http://php.net/manual/en/features.file-upload.errors.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.