Decode Base 64 in Array - php

im trying to save a base64 image into the webserver from a post request, currently my array looks like this.
(
[0] => Array
(
[id] => 0
[storename] => test
[notes] => test
[image] => data:image/jpeg;base64,/9j/4........to long
When i try to update my code to allow for decode and save all i get echo from image is a number.
PHP
$random = md5(rand());
$nonerejected[] = array(
'id' => $data['id'],
'storename' => $data['storename'],
'notes' => $data['notes'],
'image' => $data['image'] == "" ? "" : file_put_contents(''.$random.'.JPG',base64_decode($data['image'])),
);
Array output
(
[0] => Array
(
[id] => 0
[storename] => test
[notes] => test
[image] => 503331
)
)
Any ideas guys?

The data:image/jpeg;base64, bit isn't base64, so base64 can't really do anything with it. You'll have to strip this off in order to store your file.

Related

How can I get a specific value in this PHP array?

I'm setting up a Cloudinary listing of uploaded videos and I want to get the metadata of that video.
I have a print_r($result):
<pre>
Cloudinary\Api\Response Object
(
[rate_limit_reset_at] => 123123123
[rate_limit_allowed] => 123
[rate_limit_remaining] => 123
[storage:ArrayObject:private] => Array
(
[public_id] => dog
[format] => mp4
[version] => 123123123
[resource_type] => video
[type] => upload
[created_at] => 2019-07-29T07:32:50Z
[bytes] => 123123
[width] => 321
[height] => 456
[backup] => 1
[access_mode] => public
[url] => http://res.cloudinary.com/demo/video/upload/dog.mp4
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
[next_cursor] => 123123123
[derived] => Array
(
[0] => Array
(
[transformation] => /jpg
[format] => jpg
[bytes] => 86438
[id] => 123123123
[url] => http://res.cloudinary.com//demo/video/upload/dog.jpg
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
[extension] => jpg
)
[1] => Array
(
[transformation] => t_media_lib_thumb/jpg
[format] => jpg
[bytes] => 3293
[id] => 12123123
[url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
[extension] => jpg
)
)
)
)
</pre>
I tried getting the value using this format but it says undefined index. echo $result[4]['width'] or echo $result['storage:ArrayObject:private']['width'];
I talked to a cloudinary support ad he said that "you should be able to access the data returned within [storage:ArrayObject:private] by simply ignoring it and requesting the index that you desire directly from $result. Meaning you can use or and so on with the other paramters."
So basically, just removing the index solved my problem like so:
<?php echo $result['height']; ?> <?php echo $result['width']; ?>
Have a look at this https://github.com/cloudinary/cloudinary_php/blob/master/src/Api/Response.php in order to better understand the object you are getting back.
Afterwards, look at the ArrayObject documentation here https://www.php.net/manual/en/class.arrayobject.php, as your Response object extends this class. Using getArrayCopy() to transform your object into an array should be what you need.
Bear in mind that I am not familiar with the API and that there may be better options to get what you need.

cakephp rest api multiple file uploading

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.

php array building with foreach

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
);

Trying to set array variable from part of another array variable

I'm trying to set a part of array variable into a new array variable like below:
$this->data['uploadFront'] = array();
$this->data['uploadFront'] = $this->data['Card']['uploadFront'];
But I'm getting undefined index error.
The $this->data['Card'] array is like below:
Array
(
[Card] => Array
(
[name] =>
[company_name] =>
[firstname] =>
[lastname] =>
[position] =>
[location] =>
[phone] =>
[website] =>
[mobile] =>
[comp_name] => 0
[uploadFront] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[uploadBack] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
)
What could be wrong that needs to be fixed in this process?
$this->data['uploadFront'][] = $this->data['Card']['uploadFront'];
If you want to append to array, you have to use [] to show that you are appending, not assinging.
Sorry, I didn't read your post right the first time.
As Catalin told you, if your array is what you tell us, ie $this->data['Card'] = ARRAY, you're creating an unnecessary extra level: two times 'Card', so ['Card']['Card']. I propose you define $this->data as what you told is your array, and then ask to copy ['uploadFront'] to the first level of your $this->data array. In what you described you're trying to access an unexisting part of the array.
In code (probably you'll need to declare the $this->data array somewhere else in your classes, but for the sake of explaining I write it down like this):
$this->data = array
(
'Card' => array
(
'name' => '',
'company_name' => '',
...
'uploadFront' => array
(
'name' =>'',
'type' => '',
'tmp_name' => '',
'error' => '4',
'size' => '0'
)
'uploadBack' => array
(
...
)
)
$this->data['uploadFront'] = $this->data['Card']['uploadFront'];

Manipulating arrays in php

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.

Categories