I'm trying to echo an image content from an uploaded file with this code:
<?php
$imgContent = addslashes(file_get_contents($_FILES['image']['tmp_name']));
header("Content-type: image/png");
echo $imgContent;
?>
But it just shows an small empty square. I must upload and fetch a BLOB field from mysql to show on the browser, but it's not saving correctly.
As commented by Lars Stegelitz and RiggsFolly, I corrupted the file by adding addslashes on the file content. By removing it I could see the image being returned back.
I wroted that simple script just to test if I getting the image content correctly. In the real code I'm checking the file type.
Related
Sending an image in my php form, it gets saved onto my server and at the
action.php page it gets displayed. Now when I try to:
echo '<div id="image"><img src="'.$target_path.'" width="280" height="280"></div>';
it works just fine... but if I add unlink($target_path); at the end of my php code it
will not even display the image even though it gets deleted AFTER displaying the image...
So the question is, how can I display the image and deleting it at the same time so my server does not gets stuffed with user pictures?
Try another thing: output the image base-64 encoded.
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
echo '<div id="image"><img src="data:image/jpg;base64,'.$base64.'" width="280" height="280"></div>';
(instead of move_uploaded_file() etc, use as $file variable the $_FILES[...]['tmp_name'])
You can achieve this by creating a little script that gets the image-filename and will delete it after it has been retrieved:
<?php
$file = image_file_from_parameter($_GET['image']);
headers_for_file($file);
readfile($file);
unlink($file);
In your HTML output you then link to that script:
<img src="path/to/image.php?image=893sudfD983D" />
If you set nice caching headers, the user won't notice that your server did serve the file only once.
When you echo the url of an image with img src you're just sending the browser the url of an image, not the actual image data. The image needs to remain on the server if you want it to be viewable by this approach.
You could use bwoebi's solution to pass the actual image data instead of a link, but a better solution is just to keep the images on the server and periodically delete old files.
I have a script which extracts Exif data from a photo stored in a given directory and then displays the image with the exif information on a web page.
Here's the part of the script which retrieves the exif data, this example being a hard coded image:
$Image = 'original/raleighbike.JPG';
echo "$Image";
$exif = exif_read_data($Image, 0, true);
This works and the exif data is displayed.
(The 'echo' is just there to verify that the correct image is being setup for retrieval in the next part - which is where I have a problem.)
I now use the same script to fetch the exif from an image uploaded in a previous script and stored in the same directory.
The upload script sets a variable ($Image9) containing the image name and location and the exif extraction script now looks like this:
$Image = $Image9;
echo "$Image";
$exif = exif_read_data($Image, 0, true);
The problem I have is that the 'echo' prints the correct image name and location, but the exif data is empty and the error log reads:
[02-Apr-2013 18:45:29 America/Chicago] PHP Warning: exif_read_data() [<a href='function.exif-read-data'>function.exif-read-data</a>]: Unable to open file in /home2/webi8726/public_html/domain.com/sqlsidebar/displayexif.php on line 23
Line 23 is the last line in the quoted section above.
I cannot understand why the hard coded image is opened with no problem, but the image identified by the variable can't be opened - even though the variable contains the correct info and is pointing to the same file which is hard coded.
Can anyone suggest why the problem is occurring.
OK- played about some more today and this is what I find.
1 - In the script that uploads and stores the jpg I changed the variable it sets to $photo.
$photo should contain the name of the jpg and the directory it is stored in. I also set a different variable called $thumb which contains the name and location of a thumbnail of the jpg which is created and stored by the upload script.
2 - I changed the exif extraction script to use the $photo variable to create a variable called $Image which is used at various points in the script after exif extraction - for example to fetch and display the image as well store the image details in a sql database.
3 - I set some 'echo' commands to verify the contents of the variables, as well as too confirm the file exists.
This is the script code now:
$Image = $photo;
echo "$photo";
echo $Image;
echo file_exists($Image) ? 'ok' : ' image file not found';
echo file_exists($photo) ? 'ok' : ' photo file not found';
echo $thumb;
echo file_exists($thumb) ? 'ok' : ' thumb file not found';
$exif = exif_read_data($photo, 0, true);
The resulting echo string displays the following
original/raleighbike.jpg - content of variable $photo (correct)
original/raleighbike.jpg - content of variable $Image (correct)
image file not found - when checking for the file in $Image (incorrect, file does exist)
photo file not found - when checking for the file in $photo (incorrect, file does exist)
thumb file not found - when checking for the file in $thumb (incorrect, file does exist)
thumbnail/thumb_raleighbike.jpg
So it would appear that if a file is hardcoded the script is able to physically locate it, but if a file/s are specified as variables, the script cannot find them.
Has anyone any ideas as to why this might happen - I surely can't be the only one to ever experience this issue ;-(
hello friends i want to save image from my wamp server to my specific wamp project folder i have used below code ,this code show me the dialog to save image but i want to call image from wamp server and save it in my folder
<?php
header('Content-type: image/png');
$imageUrl = "http://ip/demo/images/itemimage/Platters.png";
$filename = realpath(dirname("http://ip/demo/images/itemimage/Platters.png"))."/image1.png";
$handle = file_get_contents($imageUrl);
file_put_contents($filename, $handle);
?>
this code do not download image automatically to my folde can any one help me ?
i got ths error with above code
The problem is you do two things on your script. You set the image to deliver the image over the php script.
<?php
header('Content-type: image/png');
echo file_get_contents("http://ip/demo/images/itemimage/Platters.png");
?>
Then you should fetch the image and send the image with echo to the user.
When you want to save the image then you need only fetch the image with file_get_contents and put them to your harddrive what you do at the moment.
I think in your example the echo missing this is why you get the error. You set the content type of the site but you don't deliver any png data.
<?php
header('Content-type: image/png');
$handle = file_get_contents("http://ip/demo/images/itemimage/Platters.png");
file_put_contents(dirname(__FILE__).md5(time()), $handle);
echo $handle;
?>
Edit: Try something like this. Then he should save the image in the same path.
One way:
exec("wget http://ip/demo/images/itemimage/Platters.png /tmp/demo/images/itemimage/Platters.png");
I stored it images in the database using an BLOB field (I'm using SQLite). Now I want to recover this image to a HTML page and show the images there.
I can retrieve the binary data from the image from the database, but what I can do to transform this data in an image and show in the page? Currently I want to show the images inside a field in a table.
You could abuse the data: protocol, but trust me, you don't want that if you can avoid it. Normally, you create a separate php-script that serves images, so in script 1:
<img src="/myimagescript.php?id=1234">
In myimagescript.php:
//get the data from the database somehow (mysql query et al.)
//let's assuma the data is in $data
header('Content-Type: image/jpeg');//alter for png/gif/etc.
echo $data;
#uscere90 is right, but an example might help (example of a PNG image):
<?php
header("Content-type: image/png");
echo $image_data;
?>
Typically this is done by creating a wrapper script or function that retrieves the BLOB and delivers it with the appropriate content headers to be used as an <img src=''>
Doing it this way also gives you the benefit of being able to deliver or not deliver the image based on other authentication factors determined by your PHP. If, for example, a user doesn't have permission to see an image, you can instead show some default or blocking image in its place.
// File getimg.php
// Retrieve the image blob specified by $_GET['imgid'] from the database
// Assuming your blob is now in the variable $image...
header("Content-type: image/jpeg");
// Just echo out the image data
echo $image;
exit();
Now in your html:
<img src='getimg.php?imgid=12345' alt='this is your img from the database' />
You can create a simple image.php page that queries your database, then prints out a content-type relevant to the image and vomits the binary data to screen. So, in your table, you'd have <img src=image.php?id=something />, and then you'd use that id in your image.php page to do your database lookup, retrieve the binary data, and print it to screen after printing the content-type header.
image.php:
<?php
header('Content-type: image/jpeg');
//DO SQL NINJA STUFF HERE
echo mysql_result($result,0,"file_content");
?>
There are two options I would say:
You create a script that returns the image data. The <img src="-field then calls that script.
You offer the data of the images directly via a data url.
Both have it's pros and cons. For the first solution you must create a new script for the images. The second method will bloat your page if the images are large.
As there are examples for the image script method already, here is some code fragment for data URIs:
<?php
function data_uri($content, $mime)
{
$base64 = base64_encode($content);
return ('data:' . $mime . ';base64,' . $base64);
}
?>
<img src="<?php echo data_uri($content,'image/png'); ?>" />
You need to set the mime-type according to your image, image/png for PNG images, image/jpeg for JPG files etc., see here for a list.
I am getting a raw jpg post from flash into a php script and saving the image in a temp folder in the server. The image is saved, the size is correct, but when I try to open it I get an error message saying that it is corrupted.
Here is the php code that I am using:
<?php
$data = file_get_contents('php://input');
$result = file_put_contents("imagePathOntheServer/image.jpg",$data);
echo $result;
?>
Any ideas?
Your help will be much appreciated.