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.
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 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);
}
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'm developing a tool for a client, and I save the data in a txt file when using the tool offline, so that he can then upload this file onto server and save data in database.
The data in the text file are presented like below:
Array
(
[idcategorie] => 1
[idmasteruser] => 1
[societe] => Company
[marque] => Brand
[audit] => AUdit
[nom] => Baker
[prenom] => James
[phone] =>
[email] => some#some.com
[nboutils] => 3
[outil0] => Array
(
[id] => 20
[valeurs] => Array
(
[0] => 24
[1] => 4
[2] => 27
[3] => 16
)
[file] => /newbam/images-up/HopbV_chefs.jpg
[notes] => Array
(
[0] => 4
[1] => 5
[2] => 7
[3] => 6
)
)
)
How can I put this data in a PHP array that I can handle after using keys?
If you used var_dump to generate this data, restoring it back into an array is a non-trivial task. However, this is much easier if you export the data using var_export or serialize, which can be put back into an array.
You can serialize the array:
http://php.net/serialize
It writes the array in a format that can be saved in a file and parsed later as an object or an array with unserialize:
http://php.net/unserialize
Hope it helps!
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.