how to save the image with name posted in the request - php

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

Related

Return name of created file from decoded string in php

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

Can not upload and display image in webpage with php code

I am facing some problem when I try to upload and display an image with the following php code:
<?php
header('Content-Type: bitmap; charset=utf-8');
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
?>
The image is uploaded and located in the same folder which has the php script.
Now, if I delete all of the code and replace with just 1 line
<?php
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";
?>
The image which has been uploaded, is displayed in the webpage ok.
Then, I combine both of it (uploading and displaying the image).
<?php
header('Content-Type: bitmap; charset=utf-8');
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";
?>
Uet this does not display the image, and it seems that the image has not been uploaded yet.
What is the problem and how can I solve it? I would like to upload the image and show the image on the webpage.
There is no image when you refresh the page because in that case $base=$_REQUEST['image']; will be empty (when you refresh the page you are not providing it with img upload data.)
So basically it overrides the uploaded_image.jpg with empty data.
Just check if you have provided a value for the image. In your case:
if(!empty($_REQUEST['image'])) {
$base=$_REQUEST['image'];
$binary=base64_decode($base);
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
}
echo "<img src='uploaded_image.jpg' width='500' height='300'/> ";

How to send base64 image to server - PHP

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.

Image becomes empty when uploading from JSON using POSTMAN

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

How to save many images through a script php

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……';
?>

Categories