7 Replies - 1746 Views - Last Post: 22 November 2011 - 12:34 AM Rate Topic: -----

#1 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

PHP how to upload more the one file at a time without using arrays in

Posted 20 November 2011 - 09:37 PM

Hi, how can you upload more then one file at a time.

I am able to upload one file at a time.

well, in the html I don't have the input fields as an array.

I have javascript to count them and right next to their name I would add their numbers.

Then I submit them. Now I have no clue on the php side how to do multiple files.

I do know that I need to do a for or a for each loop but I find examples only showing how to do this with input names as arrays.

I don't have them in arrays but I count them and give them a number next to their names.

so how would I do this if I didn't make the names in html arrays but actually added numbers to the name.

for example lets say the name of input is images.

in html/php it be images[]

but I have it like this images1 , images2, images3 etc.

how can I do the for loop thru them?? I want to first count what been uploaded. So I know what the max we have so I can stop the loop when we loop thru all them.

any ideas?


I want to do this without using HTML arrays.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP how to upload more the one file at a time without using arrays in

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,101
  • Joined: 18-April 07

Re: PHP how to upload more the one file at a time without using arrays in

Posted 20 November 2011 - 10:15 PM

Put multiple <input type="file"> elements in your form. All of them are uploaded and you can access them using the $_FILES array. Check out that array at PHP.net for more information. It will explain how you can do multiple files.
Was This Post Helpful? 0
  • +
  • -

#3 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: PHP how to upload more the one file at a time without using arrays in

Posted 20 November 2011 - 10:27 PM

View PostMartyr2, on 20 November 2011 - 11:15 PM, said:

Put multiple <input type="file"> elements in your form. All of them are uploaded and you can access them using the $_FILES array. Check out that array at PHP.net for more information. It will explain how you can do multiple files.



well I dynamically add the inputs inside a div.

I don't have them in array form.

I have input named like image1, image2, image3, image4.

I don't have it in an array form. Would this matter or would I still do it the same way as if I did it by using html arrays?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: PHP how to upload more the one file at a time without using arrays in

Posted 20 November 2011 - 11:16 PM

that way you would have to fetch them by each single name.

believe me, arrays are easier to work with.
Was This Post Helpful? 0
  • +
  • -

#5 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: PHP how to upload more the one file at a time without using arrays in

Posted 20 November 2011 - 11:21 PM

View PostDormilich, on 21 November 2011 - 12:16 AM, said:

that way you would have to fetch them by each single name.

believe me, arrays are easier to work with.


ya but then the problem would be how would I be able to remove them if the user clicks the delete button. If I use an array then when you hit the delete button it will delete all inputs at the same time. Instead of just deleting just that one.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: PHP how to upload more the one file at a time without using arrays in

Posted 20 November 2011 - 11:45 PM

then your way of accessing the element to delete is faulty.

let’s say you have the delete button and the input in a list
<li>
  <input type="file" name="images[]">
  <button type="button">delete</button>
</li>

by using jQuery deleting becomes as easy as
$("li button").click(function(evt) {
  $(this).parent().remove();
});

and in plain JS it ain’t that much more complicated:
function delete(evt)
{
    var p = this.parentNode;
    p.parentNode.removeChild(p);
}
(function(){
  var b = document.querySelectorAll("li button");
  for (var l = b.length; l--;)/> {
    b[l].addEventListener("click", delete, true); // or the appropriate cross-browser code
  }
})();

This post has been edited by Dormilich: 20 November 2011 - 11:46 PM

Was This Post Helpful? 2
  • +
  • -

#7 hockey97  Icon User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: PHP how to upload more the one file at a time without using arrays in

Posted 21 November 2011 - 11:25 PM

View PostDormilich, on 21 November 2011 - 12:45 AM, said:

then your way of accessing the element to delete is faulty.

letís say you have the delete button and the input in a list
<li>
  <input type="file" name="images[]">
  <button type="button">delete</button>
</li>

by using jQuery deleting becomes as easy as
$("li button").click(function(evt) {
  $(this).parent().remove();
});

and in plain JS it ainít that much more complicated:
function delete(evt)
{
    var p = this.parentNode;
    p.parentNode.removeChild(p);
}
(function(){
  var b = document.querySelectorAll("li button");
  for (var l = b.length; l--;)/> {
    b[l].addEventListener("click", delete, true); // or the appropriate cross-browser code
  }
})();


but wouldn't that delete the whole list?

I want the user to be able to delete each input file they added but not all at one time.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: PHP how to upload more the one file at a time without using arrays in

Posted 22 November 2011 - 12:34 AM

View Posthockey97, on 22 November 2011 - 07:25 AM, said:

but wouldn't that delete the whole list?

what makes you think that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1