I am trying to upload an image from JSON code.
But when it is uploaded its data is stored in the folder. But data is lost(the file exist but has 0 bytes) I don't know if the problem is exactly from where. If anyone know please help me.
This is the code
public function decodeImage($imageData)
{
$imageData = $dat->imageData;
$this->decodeImage($imageData);
$filename = "sampletest";
$binary=base64_decode($imageData);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('./'.$filename, 'wb');
fwrite($file, $binary);
fclose($file);
}
Related
Hi i have the following code to decode and save image
$photo = $_REQUEST['image'];
$binary = base64_decode ($photo);
header ('Content-Type: bitmap; charset=utf-8');
$file = fopen ('uploads/'.time().'.png', 'wb');
fwrite($file, $binary);
fclose ($file);
This works fine but i will like to get the name of the image after it is written and store that name as a variable to use in the rest of the code where it will be saved in mysql database. I can write to mysql i just need to get the file name. Thanks
You are already naming the file yourself. Just store it in a variable before you write the file.
$filename = time().'.png';
$file = fopen ('uploads/'.$filename, 'wb');
I'm sending a base64 string from an Android app to a server through a PHP file.
I got this from a tutorial video:
<?php
require 'Init.php';
header('Content-type : bitmap; charset=utf8');
if(isset($_POST['encoded_string'])){
$encoded_string = $_POST['encoded_string'];
$image_name = $_POST['image_name'];
$decoded_string = base64_decode($encoded_string);
$path = 'ProfileIcons/' .$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
}
?>
It's storing the image in the directory but when you open, it has no image. It's a blank png. Is there something wrong with the code? If so, what should I use? Thanks.
There are several points you should check:
1.Make sure the server has received the base64 string
$encoded_string = $_POST['encoded_string'];
Check the length of $encoded_string, it should have the same length as the android client says.
Make sure the decoding works
$decoded_string = base64_decode($encoded_string);
Check the length of $decoded_string, it should NOT be zero or something odd.
Make sure the file written works
$is_written = fwrite($file, $decoded_string);
$is_written should be the length of the data that has been written to the file, if it is a false, then something is wrong.
I want to upload image file into alfresco using cmis api using PHP..
I can create simple text document in alfresco using following code
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, "testssss", "text/plain");
I tried following code to upload image
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, null, "image/jpeg");
But can't create image file into alfresco
Can anyone help me to solve this issue ??
I got the solution of this problem
Just store base64 content into image.. Use below code
$filename="A.jpg";
$handle = fopen($filename, "r");
if(!$handle)return FALSE;
$contents = fread($handle, filesize($filename));
if(!$mimetype)$type=mime_content_type($filename);
else $type=$mimetype;
fclose($handle);
$base64_content=base64_encode($contents);
$obs = $client->createDocument($myfolder->id, $repo_new_file,$prop, base64_decode($base64_content), "image/jpg");
In my app android, I take photos with android camera and I send these photos to pc through this script PHP :
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>
I obtain the photo correctly to my pc, but when I send an other photo, this overwrite the previous photo..because the name ("uploaded_image.jpg") is the same.
It's possibile to assign different names so I can save all the photos ?
You can use uniqid to create a unique file name each time,
$fileName = uniqid().'.jpg';
$file = fopen($fileName , 'wb');
you can use PHP time() function. It will add timestamp with photo name.
$file = fopen(time().'uploaded_image.jpg', 'wb');
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen(time().'uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>
I have this simple php file that read the image from my android application:
<?php
$base=$_REQUEST['image'];
$cod=$_REQUEST['cod_anagrafico'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
//binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
print($binary);
$theFile = base64_decode($image_data);
$file = fopen('docs/test2.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=docs/test.jpg>';
?>
But it save my pic with the standard name "test". So i need to save this image with the name of variable "cod". How can i do that? And how can i delete the image when the process end?
Thanks
Change this line and you should be ok
$file = fopen("docs/{$cod}", 'wb');