【发布时间】:2016-06-02 08:34:18
【问题描述】:
以下脚本复制自 W3Schools http://www.w3schools.com/php/php_file_upload.asp。
脚本没有将图像上传到 uploads/ 目录 - 我的脚本有问题吗?或者是否需要实现一些额外的东西才能使脚本正常工作?
目录名称: "uploads/"
文件名: "upload.php"
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
编辑
以上错误现在来自以下脚本
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Copy the file to target folder
if ($uploadOk) {
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], target_dir . $target_file );
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
【问题讨论】:
-
检查目录是否有合适的写入权限:755
-
@R。琼斯,您是否尝试在“file_uploads = On”上配置 php.ini 文件以允许在您的服务器上上传
-
我已经联系了我的托管服务提供商,并检查了 php.ini 和 file_uploads 是否已激活。 'file_uploads = 开启'
-
@R。 Jones 从您的代码中丢失了一些将文件上传到服务器的代码,从您上面的代码中,您只是在检查是否可以上传。