【问题标题】:Having problems with uploading photo with PHP使用 PHP 上传照片时遇到问题
【发布时间】:2016-02-22 19:45:41
【问题描述】:

我正在尝试将一些图片上传到我的名为“uploads”的文件中 问题是它一直说:“。对不起,上传您的文件时出错。”我的“上传”文件位于“/Users/Ony/Documents/uploads/”中。

这是我的 html 文件:

<!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 代码:

    <?php
$target_dir = "/Users/Ony/Documents/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;

echo($target_dir);

$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 file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $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;
}
// 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.";
    }
}
?>

连我的php.ini也应该没问题,因为是这样设置的

; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

谢谢。

【问题讨论】:

  • 这是我的 PHP 版本 => 5.5.27
  • 你是在 ubuntu 还是 windows 上工作?
  • 如果在ubuntu/linux上工作,那么可能是文件夹权限的问题。
  • 我正在使用 mac osx
  • 请查看此链接以获得许可。可能这会有所帮助。 support.apple.com/kb/PH18894?locale=en_US

标签: php image-uploading


【解决方案1】:

请验证uploads 目录是否已分配write 权限。

【讨论】:

    猜你喜欢
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2018-07-31
    • 2021-07-08
    相关资源
    最近更新 更多