这是我的HTML代码 “ post.html”
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="upload" id="upload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
这是我的PHP代码“ upload.php”
<?php
target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["upload"]["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["upload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
我得到的结果是
File is an image - image/jpeg.
文件执行没有问题,但问题是,即使我成功执行了代码,只要我通过html代码上传任何文件或图像,文件都不会出现在上传文件夹中。
P.S我在wampserver上使用php7
答案 0 :(得分:3)
您应在代码行$uploadOk = 1;
之前使用以下代码行
move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file);
赞:
if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["upload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
在其他网站上处理代码时,请先了解那里写的内容,然后继续。 :-)
$check = getimagesize($_FILES["upload"]["tmp_name"]);
这行代码用于获取图像大小并进行大小验证。
答案 1 :(得分:0)
Try this :
<?php
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["upload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
$check = getimagesize($_FILES["upload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>