1

I have a form and user can upload multiple files. I am trying to get the files from files[] and return values as variables. This is the first time I am dealing with image uploads and I am pretty much stuck. Your help is much appreciated. Sincerely,

Following is my file upload form elements.

 <div class="section">
    <label for="file1" class="field-label"> 
        Upload image - <span class="small-text fine-grey"> (ONLY JPG : PNG : PDF) </span> 
    </label>
    <label class="field prepend-icon file">
        <span class="button btn-primary"> Choose File </span>
        <input type="file" class="gui-file" name="file[]" required id="file1" 
        onChange="document.getElementById('uploader1').value = this.value;">
        <input type="text" class="gui-input" id="uploader1" placeholder="no file selected" readonly>
        <span class="field-icon"><i class="fa fa-upload"></i></span>
    </label>
</div><!-- end  section -->

 <div class="section">
    <label for="file1" class="field-label"> 
        Upload another image - <span class="small-text fine-grey"> (ONLY JPG : PNG : PDF) </span> 
    </label>
    <label class="field prepend-icon file">
        <span class="button btn-primary"> Choose File </span>
        <input type="file" class="gui-file" name="file[]" required id="file2" 
        onChange="document.getElementById('uploader2').value = this.value;">
        <input type="text" class="gui-input" id="uploader2" placeholder="no file selected" readonly>
        <span class="field-icon"><i class="fa fa-upload"></i></span>
    </label>
</div><!-- end  section -->

So far I have created the following and I don't think it is working.

Following is enabled on my form. enctype="multipart/form-data" Following is part of my process .php where I send form values.

  // Here is the updated code***
 $file = $_FILES['file']['name'];
 $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $folder="uploads/";

 $filecount = count($file);
  for ($f=0; $f < $filecount; $f++){
  $rand =  rand(1000,100000)."-";
   if (move_uploaded_file($file_loc[$f],$folder.$rand.$file[$f]) ){
    //I need maximum 2 files uploaded. I guess name should be something else instead of file[0,1] such as finalname[0];
   $img_url=$file[0];
   $img_urla=$file[1];
   }

1 Answer 1

2

The problem with your process.php code is in the line

move_uploaded_file($file_loc,$folder.$file)

Here you pass $file_loc and $file, which are in your case arrays since you are dealing with multiple uploads. Also the line

$file = rand(1000,100000)."-".$_FILES['file']['name'];

is incorrect, since $_FILES['file']['name'] is an array which you can not append to a string.

You have to loop over the files and move them all to there location:

$file =$_FILES['file']['name'];
$filecount = count($file);
for ($f=0; $f < $filecount; $f++){
    $rand =  rand(1000,100000)."-";
    if (move_uploaded_file($file_loc[$f],$folder.$rand.$file[$f]) ){
        //file uploaded
    }else{
        //error in file upload
    }
}

After that your files should be uploaded successfully.


comments on updated code

So you want the first two file names and the location of these uploaded files. What you do now is add the first and second filename in the variables $img_url and $img_urla, but these are only the filenames, plus you do it many times because the code is inside the loop.
I propose to instead first store all uploaded filenames and paths in an array and then show only the first two like below:

 // Here is the updated code***
 $file = $_FILES['file']['name'];
 $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $folder="uploads/";
 $img_urls = array(); //<-- create an array of image urls

 $filecount = count($file);
 // so far so good
 for ($f=0; $f < $filecount; $f++){
   $rand =  rand(1000,100000)."-";
   if (move_uploaded_file($file_loc[$f],$folder.$rand.$file[$f]) ){
       $img_urls[] = $folder.$rand.$file[$f]; //add the file to the array.
   }
 }
 if (count($img_urls) > 1){ //check if there are at least 2 files
     echo "image 1: ".$img_urls[0]." and image 2: ". $img_urls[1];
 }
Sign up to request clarification or add additional context in comments.

11 Comments

Hello Bas van, I have tried your edited code however I am not getting any data neither I have files uploaded to the folder.
Are your php ini settings correct? Perhaps you try to upload too much or too big files, check the upload_max_file_size and post_max_size.
I can upload large single files. I am trying your corrections again and see if that works. I may have missed something.
on my form if I use single input field vs two input fields don't really matter right ? Considering name="file[]" on both input fields.
no that should not matter, perhaps you can update your question with the new code, then I can have a look at it. Does the folder where you want to store the files has write access?
|

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.