Screenshot of web page here我想在php的网页上显示下载的图片。已编写脚本将任何类型的文件上传到文件夹中,但无法在网页上显示图片。请帮忙。
上传文件并在网页上显示的php代码是
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png","pdf","mp4");
if(in_array($file_ext,$expeansions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"./file/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action = "" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "image" />
<input type = "submit"/>
<ul>
<li>Sent file: <?php echo $_FILES['image']['name']; ?>
<li>File size: <?php echo $_FILES['image']['size']; ?>
<li>File type: <?php echo $_FILES['image']['type'] ?>
//*********Code to display the image needs to go here.
</ul>
<ul>
</ul>
</form>
</body>
</html>
答案 0 :(得分:0)
假设您的文件已成功移动到目的地并且是公共目录,此处此行应显示图像:
<img src="<?= '/file/'.$file_name; ?>" alt="uploaded image">
答案 1 :(得分:0)
要显示图片,您只需要一个<img>
标记:
<li><img src="<?php echo "./file/".$file_name ?>">
注意:您必须使用正确的路径替换./file/
,具体取决于您的站点根目录。
注意2:小心将上传的文件重命名为$_FILES['image']['name']
,因为这可能会导致安全问题。