How to save many images through a script php - 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……';
?>

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'/> ";

php script get 25 image per second from android

I try to send 25 image/second to server. In my android app, i set it to send image every 40 milisecond, so every 1 second i will have 25 image was sent. But now with my php scrip, it cannot get continuosly image, it just get 1 image and i had to refresh website. This is my php script
<?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);
$path = "publib_html/";
$tmp_name = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
move_uploaded_file($tmp_name,$path.$name);
?>
How can i fix it to get continuesly 25 image per second or do you have which blog write about it?
Thank in advance

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 the image with name posted in the request

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

Categories