move_uploaded_file不起作用

时间:2015-10-01 04:19:12

标签: php file image-uploading

我正在尝试通过PHP脚本上传图片。

在脚本中,当我上传图像时所有条件都为真,但move_uploaded_file函数似乎不起作用。

可能是什么问题?

我的HTML代码是

<form id="register_form" action="" 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>

PHP脚本:

<?php
$target_dir = "/home/infibusiness1/Desktop/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;
}
}

// 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['fileToUpload']['tmp_name'], 
$target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has   
been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
echo "<br>File Name : ".$_FILES['fileToUpload']['tmp_name'];
}
}
?> 

1 个答案:

答案 0 :(得分:2)

确保Web服务器对目标目录具有写权限? 您需要知道Web服务器正在运行的用户,或者安全性不是问题,请尝试

chmod 777 /home/infibusiness1/Desktop/uploads/