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?
Related
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.
I trying to upload multiple filesby REST API in cakephp V2.3. I am running api by postman addon of chrome.
The issue is the format of array of files. Below is the format I am getting. It is hard to get data from the above array. Please guide me how set key to get value in standard format.
Array
(
[Reports] => Array
(
[name] => Array
(
[0] => Chrysanthemum.jpg
[1] => Jellyfish.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpDZoRzW
[1] => /tmp/phpVyb98b
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 879394
[1] => 775702
)
)
)
I am not entirely sure I got your question right but I think one problem lies in the data format. I assume you are using the savemany method. The data is expected in this format:
$data = array(
array('name' => 'Chrysanthemum.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/phpDZoRzW', 'error' => 0, 'size' => 879394),
array('name' => 'Jellyfish.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/phpVyb98b', 'error' => 0, 'size' => 775702)
);
Basically you are providing the data field by field instead of record by record. I don't think that cake can process this correctly.
To get data in the desired format you can either loop through the data or use the convenience method Hash that cakephp provides, e.g. by using extract on the required keys/values.
Receive the data in the right format
If you are able to change the submit-form you name the file input fields like this. The result should be the desired format.
<input type="file" name="Report.0">
<input type="file" name="Report.1">
This will result in the format:
[Reports] => Array
(
[0] => Array
(
[name] => 'Chrysanthemum.jpg'
[type] => 'image/jpeg'
)
[1] => Array
(
[name] => 'Jellyfish.jpg'
[type] => 'image/jpeg'
)
)
You should use MODELNAME.{n}.FIELDNAME as naming for form fields in Cakephp, though. So if reports is your model it would make sense having a field name for your file-field.
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.
I have an file uploading site, it has an option of uploading through urls, what I am trying to do is whenever a user uploads through url, I check my database if a file exists that was uploaded through same url it displays the download url directly instead of uploading it again.
The data sent to uploading script is in array form like:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
[1] => http://i41.tinypic.com/28cfub7.jpg
[2] => http://i41.tinypic.com/12dsa32.jpg
)
and the array used for outputing the results is in form like this:
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
[1] => Array
(
[id] => 44
[name] => 28cfub7.jpg
[size] => 105544
[descr] =>
[password] =>
[delete_id] => D392
[upload_id] => 6676FD881
)
[2] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)
Now I want is that if the link http://i41.tinypic.com/28cfub7.jpg is already upload I just add it to output array but maintain it in a order (if the link added was 2nd in array the output result should also show it in 2nd)
So what function should be used to remove the matched urls from input array and a function to add it output array in the order no.
// edited
Yes unset will do the thing but I want to maintain the order:
For example after unsetting the array looks like this:
Array (
[0] => http://i41.tinypic.com/3342r93.jpg
// [1] was removed
[2] => http://i41.tinypic.com/12dsa32.jpg
)
but the output array would be
Array
(
[0] => Array
(
[id] => 43
[name] => 3342r93.jpg
[size] => 362750
[descr] =>
[password] =>
[delete_id] => 75CE
[upload_id] => 75F45CAE1
)
// this will become [1], so how can i add another output[1] and shift other
// items after it to [2], [3] and so on...
[1] => Array
(
[id] => 45
[name] => 12dsa32.jpg
[size] => 49000
[descr] =>
[password] =>
[delete_id] => 54C9
[upload_id] => A58614C01
)
)
Well, you can add it to the output array by doing something like:
$OutArray[2] = $element;
Where $element is another Array with the id, name, size (etc...) elements.
As for removing from the array:
unset($OutArray[2]);
You may want to read Array (PHP manual).
If you have an indexed array, you can remove a value by doing:
unset ($array[2]);
If you want to add an item to an array, use this shorthand of array_push (you don't need to specify an index!):
$array[] = "new object";
All documentation is on php.net/arrays
Why don't use an if statement and/or file_exists() to see if the file is there. If you already have an array with the values then it just won't be uploaded again.