Here, is my html form :
<form method="post" action="filename.php" enctype="multipart/form-data">
<input id="file_upload" name="image[]" value="Upload" multiple="" type="file">
<button type="submit">Submit</button>
</form>
When I print $_FILES in filename.php file then I am able to get below array in PC :
Array
(
[name] => Array
(
[0] => Sket-printed-shirt.jpg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpSA6YF5
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 280515
)
)
But getting below array in smart phones like : Motorola G4 plus , G5 plus and HTC one mini 2
Array
(
[name] => Array
(
[0] => Sket-printed-shirt.jpg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] =>
)
[error] => Array
(
[0] => 1
)
[size] => Array
(
[0] =>
)
)
The reason you don't have tmp_name there is because the upload failed with error code 1, which indicates the file was too large. See: upload file error codes.
UPLOAD_ERR_INI_SIZE Value: 1; The uploaded file exceeds the
upload_max_filesize directive in php.ini.
Related
I've got a multipart form that's set to accept multiple images from a single field, defined as follows:
echo form_upload('userfile[]', set_value('userfile[]'), 'multiple');
When I run a successful form (using CodeIgniter's built-in validator), it only uploads one image (the last one in the list).
Suspicious, I did a dump of the $_FILES global when the form validated, and it showed the following:
Array
(
[userfile] => Array
(
[name] => image_three.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpZl6RHT
[error] => 0
[size] => 18236
)
)
If I go back and clear out any required field (it doesn't matter which), the same input shows the following in $_FILES:
Array
(
[userfile] => Array
(
[name] => Array
(
[0] => image_one.jpg
[1] => image_two.jpg
[2] => image_three.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpEFaVib
[1] => /tmp/phpkcXJmL
[2] => /tmp/phpoqcDql
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 43308
[1] => 60883
[2] => 18236
)
)
)
Based on the fact that the incomplete form registers all three images properly, I'm assuming that the form is defined properly, and that something in the validation engine is what's causing it. That said, my attempt to test that assumption by disabling all of the form validation code returned the one-file version of $_FILES. For the record, there are no validation rules tied to the file upload field.
Any ideas on what is causing the form to lose the additional images?
I have a html form with image (multiple images) upload which is optional
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Name:</label><input type="text" name="name"/></br>
<label>Brief:</label><input type="text" name="brf"/></br>
<label>Result:</label><input type="text" name="res"/></br>
<label>Photographs:</label><input type="file" name="file[]" accept="image" multiple="multiple" /></br>
<input type="submit" name="submit" value="Add"/></br>
</form>
I have an image upload function which is working fine with images and fails without images. I tried to split this function like
if (empty($_FILES['files']['tmp_name'])) {
//my text only function
}
else {
//my with images function
}
but no matter what I do, this does not call texonly() function
I've tried
if (empty($_FILES['files']['tmp_name'][$i]))
if (($_FILES['files']['tmp_name'] ==""))
if (empty($_FILES['files'.$i]['tmp_name']))
if (empty($_FILES['files']['size']))
if (($_FILES['files']['size'] == 0))
equels
not equels
is_uploaded_file
..etc..etc
but nothing works for me...Any help would be life saving
print_r($_FILES) output
with file
Array ( [files] => Array ( [name] => Array ( [0] => Penguins.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C:\wamp\tmp\php94CF.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 777835 ) ) )
without file
Array ( [files] => Array ( [name] => Array ( [0] => ) [type] => Array ( [0] => ) [tmp_name] => Array ( [0] => ) [error] => Array ( [0] => 4 ) [size] => Array ( [0] => 0 ) ) )
Your file tag name is file
<label>Photographs:</label><input type="file" name="file[]" accept="image" multiple="multiple" /></br>
so you check with file not files
if (empty($_FILES['file']['tmp_name'])) {
//my text only function
}
else {
//my with images function
}
you use ['name'] instead of ['tmp_name'],tmp_name is only recovered after the image has been uploaded
if (empty($_FILES['files']['name'])) {
//my text only function
}
else {
//my with images function
}
I downloaded and installed xampp for windows. When i try to upload pictures, video or music to my localhost in php i just get $_FILE['some_file'][name] i dont get [size], [type], [error] and most important to my [tmp_name]. But when i try to upload some .rar file or some document i get all these file informations. Here is what i get when i try to upload some music files:
Array
(
[name] => Array
(
[0] => Blondee – Moment (Original Mix) www.livingelectro.com.mp3
[1] => Eli & Fur – Turn The Lights Down www.livingelectro.com.mp3
[2] => Fake Forward Ft. CHRYSTAL - Brush It Off (Club Mix) www.livingelectro.com.mp3
)
[type] => Array
(
[0] =>
[1] =>
[2] =>
)
[tmp_name] => Array
(
[0] =>
[1] =>
[2] =>
)
[error] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[size] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
)
[type] and [tmp_name] is empty and [error] shows errors and [size] shows 0kb.
But when i try to upload some rar files everithing is working:
Array
(
[name] => Array
(
[0] => Aaargh.zip
[1] => afta-sans.zip
[2] => armata.zip
)
[type] => Array
(
[0] => application/octet-stream
[1] => application/octet-stream
[2] => application/octet-stream
)
[tmp_name] => Array
(
[0] => C:\xampp\tmp\php95EE.tmp
[1] => C:\xampp\tmp\php95FF.tmp
[2] => C:\xampp\tmp\php9600.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 13835
[1] => 32570
[2] => 26454
)
)
Here is my index code:
<?php
$files = $_FILES["postFile"];
echo '<pre>';
print_r($files);
echo '</pre></br></br>';
?>
If you notice in the ones where you do not get all the array occurances filled you do get all the uploaded files flagged with errors!
[error] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
1 = UPLOAD_ERR_INI_SIZE
That will be why none of the other fields contain what you expect.
You must always check the error array before doing anything with the uploaded files.
You need to check the php.ini file for this parameter and probably increase the max size.
upload_max_filesize = ?
And quite possibly this one too.
post_max_size = ?
The post_max_size will need to be large enough to cope with the upload_max_filesize * number of files uploaded + a few k to cope with other fields passed at the same time and general overhead.
......
I have file uploader
<input type="file" name="photos[]" id="files" multiple="true" />
and I need the total no of files uploaded in uploader.
echo count($_FILES['photos']['name']);
if I don't upload any file then result is 2.
if I upload 1 file then result is same
if I upload 2 file then result I got is 3
I haven't applied any increment in this.
Why this happening,I am totally confused...
This is should give a better idea on why that's happening.
When you have (for example) two of those fields and submitted it without putting any files. This is what $_FILES would look like:
Array
(
[photos] => Array
(
[name] => Array
(
[0] =>
[1] =>
)
[type] => Array
(
[0] =>
[1] =>
)
[tmp_name] => Array
(
[0] =>
[1] =>
)
[error] => Array
(
[0] => 4
[1] => 4
)
[size] => Array
(
[0] => 0
[1] => 0
)
)
)
So now, upon submission, your are counting echo count($_FILES['photos']['name']); which equates to 2. But the fields are empty.
Inside error index are actually codes, and they are interpreted here:
http://php.net/manual/en/features.file-upload.errors.php
That two 4's out there means:
UPLOAD_ERR_NO_FILE
Value: 4; No file was uploaded.
Here is my code:
HTML (Multiple Uploads):
<input type="file" name="attached_photo_1" id="attached_photo_1" />
<input type="file" name="attached_photo_2" id="attached_photo_2" />
<input type="file" name="attached_photo_3" id="attached_photo_3" />
<input type="file" name="attached_photo_4" id="attached_photo_4" />
<input type="file" name="attached_photo_5" id="attached_photo_5" />
PHP
$photo_array = array(
$_FILES['attached_photo_1'],
$_FILES['attached_photo_2'],
$_FILES['attached_photo_3'],
$_FILES['attached_photo_4'],
$_FILES['attached_photo_5']
);
Example
Now lets say someone only uploaded images for photo 1 and 2, leaving images 3, 4, and 5 with a $_FILES error code of 4 (meaning no file was uploaded).
Example Output (using <?php print_r($photo_array); ?>):
Array
(
[0] => Array
(
[name] => test.png
[type] => image/png
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/php0JwXJc
[error] => 0
[size] => 3469655
)
[1] => Array
(
[name] => test-2.jpg
[type] => image/jpeg
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpv7qFc6
[error] => 0
[size] => 1666451
)
[2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[3] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[4] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
MAIN QUESTION:
How Can I remove all of the parent array elements where the child array's [error] => 4? Thus in the example above elements [2], [3], and [4] would be deleted or unset from the array.
At least give it a try. See the following pseudocode to how to acheive this.
foreach($photo_array as $key => $values){
if(isset($key['name']))
$photo_array[$key] = $values;
}
}