Unexpected Count Value in PHP - php

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.

Related

Not able to get tmp_name ($_FILES) of file in smart phone

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.

How to simulate $_FILES image upload with PHP?

After hours and hours of scraping through the php documentation I've finally decided to ask for assistance as I'm unable to do it myself.
I have 3 URLS, all images that I'd like to download/save to the browser and set it's data into an accessible associative array just as if I were using a HTML form to upload them.
I wasn't able to emulate that properly, best I was able to achieve after reading the docs on copy, file_get_contents, file(I thought would've been the solution), fopen and readfile was the following which wound up being a fail as I hadn't uploaded the file itself like I needed, I had just put some information on an array.
If it's too difficult to understand this is what I'm trying to accomplish:
$url = array('url.com/img.jpeg','url.com/img.jpeg','url.com/img.jpeg');
foreach($url as $img){
// download all 3 images to the browser/local machine temporarily
// and access them via a variable outside of the foreach scope
// like below
}
Array
(
[0] => Array
(
[name] => Array
(
[0] => 0.24654000 150113339659797a543c31f24.jpg
[1] => 0.14997300 1503597010599f11d2249df30.jpg
[2] => 0.14997300 1503597010599f11d2249df30.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\Users\--\AppData\Local\Temp\php143.tmp
[1] => C:\Users\--\AppData\Local\Temp\php154.tmp
[2] => C:\Users\--\AppData\Local\Temp\php155.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 9425
[1] => 55560
[2] => 55560
)
)
)
I'm now taking a look at the tmp_name and it's location, maybe it'd be possible to use file_get_contents and put_file_contents to the local machine temporarily but how to read the file and get the associative arrays, that part isn't clear at all.

CodeIgniter Loses Images when Form Validates

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?

PHP upload file error code 8

I have a form with works well (upload) for many image files and with diferente size.
But some images are not uploaded to the server.
Problem images have empty $_FILE[name][tmp_name] and $_FILE[name][error] == 8.
In the same time other images (which have a larger or smaller file size) uploading properly.
Have you any ideas?
Thanks.
print_r($_FILES) for normal image
FILES:Array
(
[img] => Array
(
[name] => Array
(
[0] => 1320600215_0_284da_78d5c77a_xl.jpg
)
[type] => Array
(
[0] => image/jpeg
)
[tmp_name] => Array
(
[0] => /var/www/test/data/mod-tmp/phpoqm4qR
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 126867
)
)
)
print_r($_FILES) for problem image
FILES:Array
(
[img] => Array
(
[name] => Array
(
[0] => 94689121_1GPPZgCqPmI.jpg
)
[type] => Array
(
[0] =>
)
[tmp_name] => Array
(
[0] =>
)
[error] => Array
(
[0] => 8
)
[size] => Array
(
[0] => 0
)
)
)
Hi error code 8 means:
UPLOAD_ERR_EXTENSION
Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.
Things that may help:
check if your server has some extra security modules installed that may limit file uploading (eg.Suhosin gives a lot of these issues)
check if your max_upload_filesize and post_max_size params are properly set
try to check if this issue happens with some specific file (eg. big files, for some extensions or some filenames) or if it's completely random
if (isset($_FILES['files']) && !empty($_FILES['files']))
{
date_default_timezone_set("asia/kolkata");
$image=date("YmdHis")."_".$_FILES['files']['name'];
move_uploaded_file($_FILES['files']
['tmp_name'],"uploads/business/".$image);
}

PHP file upload missing tmp_name

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.
......

Categories