所以我试图在我的本地主机上通过 php 从表单设置图像上传,在运行代码并连接到数据库之后,我收到了上传错误。由于表单的所有其他部分在逐节检查后都可以正常工作,因此我将只添加上传输入的 html 及其相关的 php 脚本。
<input type="file" name="image" id="image-select" />
以及上传后必须处理图像上传和验证的php部分:
$image = $_FILES ['image']['name'];
$type = @getimagesize ($_FILES ['image']['tmp_name']);
//Never assume image will upload okay
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['image']['error']);
}
//Where the file will be placed
$target_path = "uploads/";
// Add the original filename to target path
$path= $target_path . basename($image);
// Check if the image is invalid before continuing
if($type === FALSE || !($type[2] === IMAGETYPE_TIFF || $type[2] === IMAGETYPE_JPEG || $type[2] === IMAGETYPE_PNG)) {
echo '<script "text/javascript">alert("This is not a valid image file!")</script>';
die("This is not a valid image file!");
} else {
move_uploaded_file($_FILES['image']['tmp_name'], $path);
}
因此,一旦代码到达上传过程,就会出现错误。我正在尝试上传一个小的PNG文件我添加的相关代码在它们出现在脚本中时也在它们各自的顺序中。