简体   繁体   English

使用PHP在服务器上的目录中显示存储的图像

[英]Displaying Image Stored in a Directory on the Server using PHP

I have an image stored in a directory on my server. 我有一个图像存储在服务器上的目录中。 Want to use PHP code to display it on the browser; 想要使用PHP代码在浏览器中显示它; 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>";
?>

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: fread不会添加代码来显示图片,它只会显示文件中的数据。您要做的是显示一个带有img标签的页面,其源指向您的图像文件,或将其投放使用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... 如果您使用第二种解决方案,则看到它指向一个php文件...如果您想控制访问权限或其他操作,则所有上传内容都将由您的PHP进行处理...

//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). 编辑:最后,可以使用数据URI方案来内联显示图片,此方法非常适合您的代码段,但由于并非所有浏览器都支持,因此可能不需要此方法(但所有专业人士都支持)。 Read more at http://www.websiteoptimization.com/speed/tweak/inline-images/ 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>';
?>

If you have just one image you can do something like that: 如果只有一张图像,则可以执行以下操作:

$image = glob("directory/*.jpg");
echo "<img src='directory/".$image."'>";

You forgot the html img tag 您忘记了html img标签

    <?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>";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM