944,068 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2071
  • PHP RSS
Nov 11th, 2009
0

Upload file using array for files

Expand Post »
Hi,

I am reading in a bunch of files to upload but they are in an array:

PHP Syntax (Toggle Plain Text)
  1. <input id="tab1file0" class="tab1Files files" type="file" name="tab1file[]" />
  2. <input id="tab1file1" class="tab1Files files" type="file" name="tab1file[]" />

To upload the files I call a function uploadFile() which looks like this:

PHP Syntax (Toggle Plain Text)
  1. function uploadFile($thefile) {
  2. $base_path = "files/";
  3. $target_path = $base_path . basename( $_FILES[$thefile]['name']);
  4. echo($_FILES[$thefile]['tmp_name']);
  5. if(move_uploaded_file($_FILES[$thefile]['tmp_name'], $target_path)) {
  6. echo "The file ". basename( $_FILES[$thefile]['name'])." has been uploaded <br>";
  7. } else{
  8. echo "There was an error uploading the file".basename( $_FILES[$thefile]['name']).", please try again!<br>";
  9. }
  10. }

The only problem is when I get the files from the form, they are in an array and I can't seem to point the correct string to the upload file function.

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dpatz is offline Offline
7 posts
since Mar 2008
Nov 12th, 2009
0
Re: Upload file using array for files
Click to Expand / Collapse  Quote originally posted by dpatz ...
Hi,

I am reading in a bunch of files to upload but they are in an array:

PHP Syntax (Toggle Plain Text)
  1. <input id="tab1file0" class="tab1Files files" type="file" name="tab1file[]" />
  2. <input id="tab1file1" class="tab1Files files" type="file" name="tab1file[]" />

To upload the files I call a function uploadFile() which looks like this:

PHP Syntax (Toggle Plain Text)
  1. function uploadFile($thefile) {
  2. $base_path = "files/";
  3. $target_path = $base_path . basename( $_FILES[$thefile]['name']);
  4. echo($_FILES[$thefile]['tmp_name']);
  5. if(move_uploaded_file($_FILES[$thefile]['tmp_name'], $target_path)) {
  6. echo "The file ". basename( $_FILES[$thefile]['name'])." has been uploaded <br>";
  7. } else{
  8. echo "There was an error uploading the file".basename( $_FILES[$thefile]['name']).", please try again!<br>";
  9. }
  10. }

The only problem is when I get the files from the form, they are in an array and I can't seem to point the correct string to the upload file function.

Thanks!
you are looking for method like below -
PHP Syntax (Toggle Plain Text)
  1. extract($_POST);
  2.  
  3. foreach($tab1file as $key=>$value)
  4. {
  5. //call your function here
  6. }
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need help building an instant messaging system with AJAX/PHP
Next Thread in PHP Forum Timeline: Javascript not working in PHP class file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC