php - 这是一个简单的 php 代码,它显示从目录中选择的图像,但是当我运行它时,它显示撕裂的图像并且不显示实际图像
问题描述
这是一个简单的 php 代码,它显示从目录中选择的图像,但是当我运行它时,它显示撕裂的图像并且不显示实际的图像..
这是我得到的输出
有谁知道为什么会这样?我试图在所有浏览器中运行它,它仍然显示
这是代码:
<html>
<head>
<title>PHP File Upload example</title>
</head>
<body style="background-color:cyan;">
<style>
p
{
background-color: yellow;
border: 3px solid black;
text-align: center;
}
#grad
{
background-image: linear-gradient(indigo,violet,cyan);
text-align: center;
}
</style>
<div id = "grad">
<b>
<form action="p10a.php" enctype="multipart/form-data" method="post">
Select image from dir :
<input type="file" name="file"><br/><br>
<input type="submit" value="Upload" name="Submit1"> <br/>
</b>
</form><p>
<?php
if(isset($_POST['Submit1']))
{
$target_dir = "C:/xampp/htdocs/LAMP LAB";
$target_file1 = basename($_FILES["file"]["name"]);
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";?><br><?php
$file = $target_file1;
$filesize = filesize($file); // bytes
$filesize = round($filesize / 1024, 2); // kilobytes with two digits
echo "The size of your file is $filesize KB.";?><br><?php
echo "Content last changed: ".date("F d Y H:i:s.", filemtime($file));?></p>
<br><br><br><br><b><?php
echo "<img src='C:/xampp/htdocs/LAMP LAB".$imageFileType."' width='200'> "; //display image
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
</body>
</html>
[这是我得到的输出]
解决方案
使用以下代码。
<html>
<head>
<title>PHP File Upload example</title>
</head>
<body style="background-color:cyan;">
<style>
p
{
background-color: yellow;
border: 3px solid black;
text-align: center;
}
#grad
{
background-image: linear-gradient(indigo,violet,cyan);
text-align: center;
}
</style>
<div id = "grad">
<b>
<form action="" enctype="multipart/form-data" method="post">
Select image from dir :
<input type="file" name="file"><br/><br>
<input type="submit" value="Upload" name="Submit1"> <br/>
</b>
</form><p>
<?php
if(isset($_POST['Submit1']))
{
$target_dir = "LAMP LAB/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
}
else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
echo $target_file;
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}
else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["file"]["name"])). " has been uploaded.";
echo "Content last changed: ".date("F d Y H:i:s.", filemtime($target_file));?></p>
<br><br><br><br><b><?php
echo "<img src='".$target_file."' width='200'> "; //display image
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
</body>
</html>
推荐阅读
- java - 错误:无法解决:com.github.kinvey:android-sdk:v4.3.2
- spring-boot - 如何正确配置 Spring Boot 项目以使用 Quartz 和 MySQL?
- javascript - 如何使用nestjs获取websocket url以及如何在邮递员中进行测试?
- javascript - Javascript,通过 2 个事件更新值并连接
- excel - 当更改行高或列宽时,是否有办法动态更改复选框的大小和位置?
- c# - Winform 设计 X64 不起作用“变量 usercontrol1 未声明或从未分配”
- php - 无法通过动态和单独单击复选框来显示另一个复选框
- bash - 每次使用 psql 手动输入密码的问题
- c# - 内存中的string.Empty和null有什么区别
- ios - 在 Flutter 插件中从 Objective-C 加载原生 C 库