【问题标题】:Cannot post php file - file upload无法发布 php 文件 - 文件上传
【发布时间】:2017-01-26 15:08:42
【问题描述】:

我有以下文件(html 和 php),并且能够在我的服务器上运行 php,但是当我单击上传时,我不断收到“CANNOT POST /fileUpload.php”。任何关于正在发生的事情的想法将不胜感激。谢谢

(上传.html)

<!DOCTYPE html>
<html>
<body>
<form action="fileUpload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <!--<input type="hidden" name="MAX_FILE_SIZE" value="500"> -->
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

(文件上传.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;
    }
}
// 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 html post file-upload


    【解决方案1】:

    尝试检查fileUpload.php的目录

    你不想在 if(isset($_POST["submit"])) 里面有那个移动过程吗?

    【讨论】:

    • html 文件和 php 文件都在同一个目录下,有一个名为 'uploads' 的文件夹用于存放照片。这是正确的吗?
    • ok 试试
    • 很遗憾没有。评论下方的人说代码对他来说工作正常,所以我不确定可能有什么问题
    【解决方案2】:

    您的代码运行良好。我能够上传 JPG、JPEG、PNG 和 GIF 文件。请检查您的文件结构。

    上传前可以使用file_exists()函数检查文件目标路径

    http://php.net/manual/en/function.file-exists.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 2012-11-09
      • 2013-02-10
      • 1970-01-01
      相关资源
      最近更新 更多