Skip to content
Advertisement

does multiple upload follow the same order of uploading in which we select the images?

i am using HTML5 multiple upload feature.

<input name='upload[]' type=file multiple="multiple" id="profileImages" />

its working perfect. Now I have one query

does multiple uploading follow any order while uploading images?

suppose I have 5 images

a.png
b.png
c.png
d.png
e.png

now while uploading images if I select images in below order

a.png, 
b.png,
e.png,
d.png,
c.png

does these images would be uploaded in the same order I select?

means $_FILES['upload']['name'] array will have (using PHP )

[0] =>'a.png',
[1] =>'b.png',
[2] =>'e.png',
[3] =>'d.png',
[4] = >'c.png'

or it depends upon filesize or browser or any other factor/attribute ?


sidenote

further i m storing these images individually in profile_image_table

id(pk ) | profile_id | image_name

answer of above question will make my work easy because if multiple image upload in the order we select then it would be easier to choose one image for main profile image ( using MIN(id) or MAX(id) ) of given profile_id.

Advertisement

Answer

HTML5 has nothing to do with file uploads where the server is concerned. Period. There is no reliable “order.” As the comments have noted, the actual order resulting from the new HTML5 multiple fields can vary from browser to browser. To understand why, we need to look at what happens when a user-agent (browser) submits a form with multiple file fields to a PHP server.

When you upload multiple files from an HTML form your browser is doing a couple of things automatically for you:

  1. Sending a Content-Type: multipart/form-data header to inform the remote HTTP server that it’s sending several discrete “blocks” of information within the context of this single request.
  2. Building a raw HTTP message that conforms to the HTTP specification to transport your files using the HTTP protocol.

The raw HTTP message will look something like this:

POST /somePath HTTP/1.1
User-Agent: my-browser-v0.1
Host: www.example.com
Content-Length: 42
Content-Type: multipart/form-data; boundary=--------------------

------------------------------
Content-Disposition: form-data; name="file[]" filename="file1.txt"
Content-Type: text/plain

these were the contents of file1.txt
------------------------------
Content-Disposition: form-data; name="file[]"; filename="file2.txt"
Content-Type: text/plain

these were the contents of file2.txt

It’s completely up to the browser in which order it places the data from your form. User-Agent sniffing is not really a viable option to determine the ordering. Instead, if the order of the fields is that important (pointless idea, but whatever), you should use a form in which there are a finite number of possible file fields and name them individually. This way you can access the files by their field names in the $_FILES array. After all, you aren’t required to use the new HTML5 feature just because it exists:

<form action='' method='post' enctype='multipart/form-data'>
  <input name='file1' type=file>
  <input name='file2' type=file>
  <input type='submit'>
</form>

And in your PHP file access the fields directly by their names. You’ll know which form field name was first because you coded the HTML that way:

<?php
$_FILES['file1'];
$_FILES['file2'];

Perhaps a better solution in your case would be to simply have one file field that is named directly (so you always know it’s the primary field) and then add the multi-select field for users to add further files.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement