Techie
Techie

Reputation: 45124

Multiple File Upload Validation - PHP, Joomla

I'm using Joomla 2.5.

I have 3 input fields in my program which allows user to upload similar design images.

<input type="file" name="design_image[]" class="image_upload" accept="image/*"/>
<input type="file" name="design_image[]" class="image_upload" accept="image/*"/>
<input type="file" name="design_image[]" class="image_upload" accept="image/*"/>

Above 3 input fields are dynamic. There is a dropdown which allows user to determine how many designs are to be uploaded. if user selects two, 2 input fields are created. if user selects three, 3 input fields are created. I'm doing it using jquery.

What I want to do is how do I make sure that user has updated correct number of files?

EG: if user selects two -> there should be 2 files uploaded, if user selects three - > there should be 3 files uploaded

Currently what I'm doing is using PHP is

$file = JRequest::getVar('design_image', null, 'files', 'array');
if(empty($file['tmp_name'])){
            $this->_app->enqueueMessage( JText::_('Error'), 'error');
} 

this works fine for a single file upload. How can I adjust my validation according to the user input?

Upvotes: 0

Views: 1376

Answers (2)

rynhe
rynhe

Reputation: 2529

Try this

$file = JRequest::getVar('design_image', null, 'files', 'array');

if(empty($file['tmp_name'][0])){
       $this->_app->enqueueMessage( JText::_('Error'), 'error');
}

Upload

$count = count($file['name']);
for($i=0;$i<$count;$i++) {
 if($file['error'][$i] == 0)
 {
    if($file['size'][$i] <= 8388608)   //8388608 byte = 8 mb
    {
       JFile::upload($file['tmp_name'][$i],$your_new_path)
    }
 }

Upvotes: 0

Marc B
Marc B

Reputation: 360702

PHP's array-based file upload support has a moronic way of building the $_FILES array.

normal single file:

$_FILES = array(
    'fieldname_in_file_input' => array(
        'name' => 'somefile.txt'
        'size' => 12345
        'error' => 0
        etc...
    )
);

array-based:

$_FILES = array(
   'fieldname_in_file_input' => array(
       'name' => array(
           0 => 'first_file.txt';
           1 => 'second_file.txt'
           etc..
       ),
       'size' =>
           0 => 1234, // size of 1st file
           1 => 2345, // size of 2nd file
           etc...
       ), etc...
    )
)

Upvotes: 1

Related Questions