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'/> ";
Related
Hi every on i write a code to save title and image name in the database but in my php code, i can save title text and image name in database but the image cannot be save in the folder(uploadedimages),
here is my code:
<?php
include 'config.php';
$title = $_POST['title'];
$base=$_REQUEST['image'];
$filename = $_REQUEST['filename'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploadedimages/'.$filename, 'wb');
fwrite($file, $binary);
fclose($file);
mysql_query("insert into test_upload(title,img) values('$title','$filename' )");
echo "[".$title."]send";
?>
Add the following code
$filename = $_FILES['filename']['name'];
if (move_uploaded_file($_FILES['filename']['tmp_name'], 'uploadedimages/.'$filename))
{
echo "upload succeed";
} else {
echo "Upload failed";
}
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);
}
I'm trying to load an image, edit it, save it and then display it from a script that is called within IMG tags. The script works if I want to just display the image and it does save the image. But it won't save it and then display it. Does anyone know what I'm doing wrong? Any help would be greatly appreciated.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = #imagecreatefrompng($file_location);
// This section of code removed as doesn't affect result
imagepng($img_display, $file_location);
chmod($file_location, 0777);
imagepng($img_display);
imagedestroy($img_display);
}
?>
Try this and checks if your folder has permission to save the image. chmod 777 on it for sure.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = imagecreatefrompng($file_location); // Create PNG image
$filename = $file_location + time(); // Change the original name
imagepng($img_display, $filename); // Saves it with another name
imagepng($filename); // Sends it to the browser
imagedestroy($img_display); // Destroy the first image
imagedestroy($filename); // Destroy the second image
}
Try this:
ob_start();
imagepng($img_display);
$contents = ob_get_clean();
file_put_contents($file_location, $contents);
echo $contents;
imagedestroy($img_display);
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');