Trying to upload multiple files at once

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gm2
Forum Newbie
Posts: 10
Joined: Fri Sep 24, 2010 10:55 am

Trying to upload multiple files at once

Post by gm2 »

Hi All. I am trying to implement a way to upload multiple files. I am using a file input in the form with multiple=true with the hopes of eventually being able to open a folder and select a bunch of images, and it upload all of them. Thus far, I have been unsuccessful.

Here is the form code that I am using, it seems to be working, as I can select multiple files, and hit upload.

Code: Select all


        <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONFIG['uploads']['max_file_size']; ?>"/>
        <label>File to upload</label>
        <input class="file" multiple="true" name="document[]" type="file"/>

Here is where the problem is, I believe. I don't think I am properly bringing in the array of files...

Code: Select all

    function add_file($client_id, $desc, $project, $phase, $uploaded_by)
    {
        global $CONFIG;

        /**
         * Ensure te file was uploaded,
         * fail otherwise
         */
        if (!is_uploaded_file($_FILES['document[0]']['tmp_name']))
        {
            trigger_error('Upload Error: ' . 'Error ' . $_FILES['document['.$i.']']['error'], E_USER_NOTICE);
            return false;
        }
        else
        {
            /**
             * Get all necessary information
             * about the file
             */
				$i = 0;
				 while ($_FILES['document['.$i.']']['name']) 
				 {
					$file['temp'] = $_FILES['document['.$i.']']['tmp_name'];
					$file['name'] = $this->rename_file($_FILES['document['.$i.']']['name']);
					$file['size'] = $_FILES['document['.$i.']']['size'];
					$file['type'] = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['document['.$i.']']['type']);
					$file['type'] = strtolower($file['type']);
					$file['ext'] = $this->file_extension($_FILES['document['.$i.']']['name']);
					$file['size'] = round($file['size'] / 1024, 2);
					$file['name'] = preg_replace("/\s+/", "_", $file['name']);
					/**
					 * Move the file from the temp directory
					 * to the upload directory
					 */
					if (!copy($file['temp'], $CONFIG['uploads']['path'] . $file['name']))
					{
						if (!move_uploaded_file($file['temp'], $CONFIG['uploads']['path'] . $file['name']))
						{
							trigger_error('Upload Error: ' . 'Error moving from temp dir', E_USER_NOTICE);
							exit;
							return false;
						}		
					}
					/**
					 * Create a db record for the uploaded document
					 */
					if ($this->insert_asset_row($client_id, $project, $phase, $uploaded_by['id'], $desc, $file['ext'],
						$file['size'], $file['name']))
					{

						$file_id = mysql_insert_id();

						if ($this->get_type($file['ext']) == 'image')
						{
							$this->create_thumb($CONFIG['uploads']['path'] . $file['name'], $CONFIG['uploads']['path'] . 'thumbs/' . $file['name'], 120);
						}
					}
					
					$i++;
					if (!$_FILES['document['.$i.']'])
					{return $file_id;}
					
				}
				return false;
				
		}
    }

Thanks for any help!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Trying to upload multiple files at once

Post by John Cartwright »

It's not possible to natively upload multiple files from a single selection box. You either need to create multiple upload elements, or use a flash uploader (with a PHP upload script). I would personally use a flash uploader, as it's far more flexible and intuitive to the user. If you google around you'll find plenty.
gm2
Forum Newbie
Posts: 10
Joined: Fri Sep 24, 2010 10:55 am

Re: Trying to upload multiple files at once

Post by gm2 »

Thanks. I guess that explains why it has not been working!
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Trying to upload multiple files at once

Post by shawngoldw »

John Cartwright wrote:It's not possible to natively upload multiple files from a single selection box. You either need to create multiple upload elements, or use a flash uploader (with a PHP upload script). I would personally use a flash uploader, as it's far more flexible and intuitive to the user. If you google around you'll find plenty.
This is not entirely true John, multiple file uploads are part of html 5 and currently supported by the latest versions of Chrome, Firefox, and Safari. Sadly IE9 beta still doesn't support it so it will still be a while before it can broadly be used.

Shawn
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Trying to upload multiple files at once

Post by John Cartwright »

shawngoldw wrote:
John Cartwright wrote:It's not possible to natively upload multiple files from a single selection box. You either need to create multiple upload elements, or use a flash uploader (with a PHP upload script). I would personally use a flash uploader, as it's far more flexible and intuitive to the user. If you google around you'll find plenty.
This is not entirely true John, multiple file uploads are part of html 5 and currently supported by the latest versions of Chrome, Firefox, and Safari. Sadly IE9 beta still doesn't support it so it will still be a while before it can broadly be used.

Shawn
Your right, but I wouldn't be relying on HTML5 standards and/or the major browsers compliance just yet, *cough*.
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: Trying to upload multiple files at once

Post by shawngoldw »

John Cartwright wrote: Your right, but I wouldn't be relying on HTML5 standards and/or the major browsers compliance just yet, *cough*.
Definitely agreed.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Trying to upload multiple files at once

Post by requinix »

Yay HTML 5! Too bad it won't be commonly adopted until IE 13 comes out with nearly complete support for it...

Anyways, assuming multiple uploads do work with your browser,

Code: Select all

$_FILES['document[0]']['tmp_name']
isn't the right way to get them. The name is still "document", but the tmp_name/type/etc are all arrays.

Code: Select all

$_FILES["document"]["tmp_name"][0]
gm2
Forum Newbie
Posts: 10
Joined: Fri Sep 24, 2010 10:55 am

Re: Trying to upload multiple files at once

Post by gm2 »

oh... so there is potential for this to work?

This is part of a back-end update process, and I am one of 3 guys who would ever need to upload using this. And, we are all running firefox/chrome...

So far I am having little luck implementing any form of uploader that allows multiple files, and still allows me to do some SQL input.
gm2
Forum Newbie
Posts: 10
Joined: Fri Sep 24, 2010 10:55 am

Re: Trying to upload multiple files at once

Post by gm2 »

tasairis wrote:Yay HTML 5! Too bad it won't be commonly adopted until IE 13 comes out with nearly complete support for it...

Anyways, assuming multiple uploads do work with your browser,

Code: Select all

$_FILES['document[0]']['tmp_name']
isn't the right way to get them. The name is still "document", but the tmp_name/type/etc are all arrays.

Code: Select all

$_FILES["document"]["tmp_name"][0]
Horray! That worked. Thanks a ton.

I am, however, limited to 9 files at a time. Anyone know how to change that?
gm2
Forum Newbie
Posts: 10
Joined: Fri Sep 24, 2010 10:55 am

Re: Trying to upload multiple files at once

Post by gm2 »

Upon further testing, it is not limited to 9 files, but rather a limitation number of cumulative characters in the filenames. For example, I can select many files with short filenames, or just a few with long filenames. Not sure how to correct this.
Post Reply