I'm making a class that lists files of a given extension inside a folder.
the trouble I'm having is that some files are images and I want to extract more information from those files and not for files that are not images.
I created this foreach that adds an entry to an array. later that array is returnd and contains all the files:
$file_array[] = array(
"basename"=>$file->getBasename(),
"size"=>$file->getSize(), //in bytes
"extension"=>$extension,
"file"=>$filename,
"path"=>$file->getPath(),
in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? array("info"=>getimagesize($filename)) : null
);
As i said earlier, this works.
the problem that I want to correct is the output:
Array
(
[0] => Array
(
[basename] => 1.JPG
[size] => 56533
[extension] => jpg
[file] => folder\1.JPG
[path] => folder
[0] => Array
(
[info] => Array
(
[0] => 897
[1] => 616
[2] => 2
[3] => width="897" height="616"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
)
)
[1] => Array
(
[basename] => 2.JPG
[size] => 56533
[extension] => jpg
[file] => folder\2.JPG
[path] => folder
[0] => Array
(
[info] => Array
(
[0] => 897
[1] => 616
[2] => 2
[3] => width="897" height="616"
[bits] => 8
[channels] => 3
[mime] => image/jpeg
)
)
)
)
the problem I want to correct is that I want to access the file info like $file_array[0]['info'] and not $file_array[0][0]['info']. That second index is useless since there can be only one info for a file.
Any way to correct this?
Then don't add an array around your info key:
'info' => in_array(...) ? getimagesize($filename) : null
$file_array[] = array(
"basename"=>$file->getBasename(),
"size"=>$file->getSize(), //in bytes
"extension"=>$extension,
"file"=>$filename,
"path"=>$file->getPath(),
"info" => in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? getimagesize($filename) : array()
);
This should work
Your keys are incorrect. Try this. Note that I've added info as an index which sets the value from the condition.
$file_array[] = array(
"basename"=>$file->getBasename(),
"size"=>$file->getSize(), //in bytes
"extension"=>$extension,
"file"=>$filename,
"path"=>$file->getPath(),
'info' => in_array(strtolower($extension), array("jpg","jpeg","png","gif")) ? getimagesize($filename) : null
);
Related
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've an associative array called $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[pt_doc_title] => Array
(
[1] => Test Document
[2] => New Joining
[3] => Hallo Jolly
)
[pt_doc_id] => Array
(
[0] => 6
[1] => 7
)
[submit] => Update
)
In order to keep all the package type documents data together I've manipulated the above array as follows:
foreach ($data['pt_doc_title'] as $key => $title) {
$id = isset($data['pt_doc_id'][$key-1]) ? $data['pt_doc_id'][$key-1] : null;
$data['pt_documents_data'][] = array(
'pt_doc_title' => $title,
'pt_doc_id' => $id
);
}
unset($data['pt_doc_title'], $data['pt_doc_id']);
After manipulation I'm getting following array $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
)
)
)
My issue is I'm haivng another array called $_FILES as follows and I want to merge one of it's key(name) into above array in a same manner.
Array
(
[document_file_name_1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_3] => Array
(
[name] => FAQ.doc
[type] => application/msword
[tmp_name] => /tmp/phpFiBYKB
[error] => 0
[size] => 35840
)
)
That is if there exists a value under [name] then the final array should be as follows. As there is a value present only in last array element of array $_FILES
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
[pt_doc_file_iname] =>
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
[pt_doc_file_iname] =>
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
[pt_doc_file_iname] => FAQ.doc
)
)
)
Can anyone please help me in creation of such final array?
Simplest way would be to walk the $_FILES array and extract the id from the last segment of the field name... then use that -1 as the basis for adding the file to your result array.
Something like this should get you there (untested):
foreach($_FILES as $k => $file){
$index_to_update = trim(substr($k, strrpos($k, "_")+1))-1;
$res["pt_document_data"][$index_to_update]["pt_doc_file_iname"] = isset($file["name"])?$file["name"]:"";
}
That is assuming $res is the array that is the parent of the pt_document_data element.
am unable to use the path returned by drupal 7 for an image which was uploaded for custom content type. the path returned is as follow:
Array ( [und] => Array ( [0] => Array ( [fid] => 6 [alt] => [title] => [width] => 464 [height] => 261 [uid] => 1 [filename] => _71135631_c0166859-snotty_child_with_a_cold-spl.jpg [uri] => public://_71135631_c0166859-snotty_child_with_a_cold-spl.jpg [filemime] => image/jpeg [filesize] => 22168 [status] => 1 [timestamp] => 1384797114 [rdf_mapping] => Array ( ) ) ) )
but when using the vale of uri, the image is not shown.
any assistance appreciated.
regards,
a.ali
Such uri-fields in Drupal always refer to the internal data-storage logic, hence the public:// at the beginning. Drupal uses Image Styles for any images attached to content.
To retrieve the full URL for an image, you should use image_style_url():
$imgPath = image_style_url('default', $object['und'][0]['uri']);
I have a json file. The json file contains the path of some images. I want to read the url path and save the image in my computer. I was able to read the json file but can't get the image_path object in the json. My json file is of the format:
Array
(
[0] => Array
(
[product] => Array
(
[product_id] => 262
[product_name] => VD0289 CUT OUT BACK DRESS
[product_inventory] => 2
[product_price] => B
[label_name] => 15
[product_status] => A
[images] => Array
(
[0] => Array
(
[image_id] => 935
[image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289.jpg
[image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289.jpg
[image_type] => M
)
[1] => Array
(
[image_id] => 938
[image_path] => http://gird.com/images/thumbnails/0/400/500/VD0289_VD0289_(4).jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/VD0289_VD0289_(4).jpg
[image_detailed] => http://gird.com/images/detailed/0/VD0289_VD0289_(4).jpg
[image_type] => A
)
)
[options] => Array
(
)
)
)
[1] => Array
(
[product] => Array
(
[product_id] => 263
[product_name] => Chic Chanel Inspired Dress - Blue
[product_inventory] => 1
[product_price] => O
[label_name] => 9
[product_status] => A
[images] => Array
(
[0] => Array
(
[image_id] => 939
[image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-1.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-1.jpg
[image_detailed] => http://gird.com/images/detailed/0/100678-Blue-1.jpg
[image_type] => M
)
[1] => Array
(
[image_id] => 942
[image_path] => http://gird.com/images/thumbnails/0/400/500/100678-Blue-4.jpg
[image_thumbnail] => http://gird.com/images/thumbnails/0/60/60/100678-Blue-4.jpg
[image_detailed] => http://gird.com/images/detailed/0/100678-Blue-4.jpg
[image_type] => A
)
..............................
How can I get all the image path and image_detailed from this file?? My php code is:The code is to only get the first image path.
<?php
$file="dw.json";
$json= json_decode(file_get_contents($file),true);
print_r ($json[0]["product"]["images"][0]["image_path"]);
//print_r($json);
?>
Try this out:
foreach($json as $key => $products) {
$product = $products['product'];
foreach($product['images'] as $key => $image) {
$product_images[$product['product_name']][] = $image['image_path'];
}
}
print_r($product_images);
Use a for loop, like this:
foreach ($json[0]["product"]["images"] as $key)
echo $key['image_path'];
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.