1

I have an image stored in a directory on my server. Want to use PHP code to display it on the browser; something is wrong with my code. Please help.

<?php
$image = fopen('upload/foto4.JPG', 'r');
$Data = fread($image,filesize('$image'));
fclose($image);
echo"<div style='width:15%;height:10%;position:relative;top:22%;left:20%'/>".$Data."</div>";
?>
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
chuzymatics
  • 39
  • 3
  • 8
  • only 1 image you want to display or all images in directory ? – DelphiStudent Dec 26 '14 at 09:13
  • Please see http://stackoverflow.com/questions/1851849/output-an-image-in-php – Laplace Dec 26 '14 at 09:16
  • only 1 image @DelphiStudent – chuzymatics Dec 26 '14 at 09:16
  • for that i see the code works perfect but you forget to put `` tag . better yet you can use `header('Content-type: image/jpg');` – DelphiStudent Dec 26 '14 at 09:20
  • use tag.. it works fine than this to display an image in the browser... ``..... use this instead of using `fopen` and `fread` functions – annam priyatam Dec 26 '14 at 09:21
  • Do not use `r` mode. Use `rb` instead. If the mode is set to `r`, Windows will translate the newlines to the right ones. This will cause LOTS of problems with binary data. Using `rb` tells php to read the fine in binary mode which disables that translation and you are problem-free! Read here: http://php.net/manual/en/function.fopen.php – Ismael Miguel Dec 26 '14 at 09:32

3 Answers3

2

fread will not add the code to display the picture, it only shows the data that is in the file... What you want to do is display a page with an img tag, and its source pointing to your image file, or serve it using php:

echo"<div style='width: 15%; height:10%; position:relative; top:22%; left:20%'/><img src='uploads/foto4.JPG'/></div>";
// Or
echo"<div style='width: 15%; height:10%; position:relative; top:22%; left:20%'/><img src='uploads.php?f=foto4.JPG'/></div>";

If you use the second solution, see it points to a php file... All your uploads would be handled by your PHP that way if you want to control access or other stuff...

//uploads.php
header('Content-Type: image/jpeg'); // We are serving a jpeg. 
readfile('uploads/'.$_GET['f']);

EDIT: Finally, one could use the data URI scheme to show the picture inline, this method would fit perfectly for your code snippet but might not be required as not all browsers support it (but all majors do). Read more at http://www.websiteoptimization.com/speed/tweak/inline-images/

<?php
$image = fopen('upload/foto4.JPG', 'rb');
$Data = fread($image,filesize('$image'));
fclose($image);
echo'<div style="width:15%;height:10%;position:relative;top:22%;left:20%"/><img src="data:image/jpeg;base64,'.base64_encode($Data).'"/></div>';
?>
Salketer
  • 14,263
  • 2
  • 30
  • 58
  • DO NO USE `file_get_contents()` WHEN SENDING A FILE TO THE BROWSER!!! It will eat up your memory and the script will stop if the file is bigger than `memory_limit` on the `php.ini` file. Use `readfile()`instead. Doc: http://php.net/manual/en/function.readfile.php – Ismael Miguel Dec 26 '14 at 09:29
  • Cool thanks for your comment. I always forget about readfile. Changing my answer. – Salketer Dec 26 '14 at 09:32
  • You are welcome. Just 2 things in your answer: 1 - You aren't validating the file name. You can use the regex `/^\w+\.(?:jpe?g|png|gif)$/` to validate it, using `preg_match()`. 2 - You can use `uploads.php/foto4.JPG` instead in the `src`attribute and you check the file at the `$_SERVER['PATH_INFO']` variable, which looks a lot more 'badass'. You can check https://php.net/manual/en/reserved.variables.server.php to see the usage. – Ismael Miguel Dec 26 '14 at 09:37
  • @IsmaelMiguel this would make a more complicated answer and require more setup from OP. Your 2 points are very valid, but if OP cannot make it work without those additions, they will not help display an image. Also your regexp does not allow for folders, which prevents the user to use ../foto4.JPG but also prevents the coder to use folders as he'd like... – Salketer Dec 26 '14 at 09:41
  • To solve the issue with folders, one can use `~^(?:\w+/)*\w+\.(?:jpe?g|png|gif)$~`. And I see your point. Also, allowing `../photo.jpg` is like requesting for a huge security breach. One can easily upload a file and place it anywhere he decides it to be placed and read from it. This is assuming that the code is for uploads. Other than that, I repeat, you are right about it. – Ismael Miguel Dec 26 '14 at 09:49
  • One thing: On your 2nd example, you provide the MIME type `image/gif` to the `data:` URI. It should be `image/jpg` for it to work. – Ismael Miguel Dec 26 '14 at 09:55
-1

If you have just one image you can do something like that:

$image = glob("directory/*.jpg");
echo "<img src='directory/".$image."'>";
soroush gholamzadeh
  • 2,616
  • 1
  • 22
  • 30
-2

You forgot the html img tag

    <?php
$image = fopen('upload/foto4.JPG', 'r');
$Data = fread($image,filesize('$image'));
fclose($image);
echo"<div style='width: 15%; height:10%; position:relative; top:22%; left:20%'/><img src="". $Data."" /></div>";
?>
  • fread return the binary content of the file, the img src cannot do anything with that... – Salketer Dec 26 '14 at 09:26
  • 1
    Your code won't work for 2 reasons: 1- You are using `"` to start a string and then you have `` in your src attribute. You will get a syntax error saying that php expects `)` but found `"`. 2- You are simply throwing the binary data read directly from the file and expect it to work. You need a `data:[...]` URI to work. – Ismael Miguel Dec 26 '14 at 09:52