【问题标题】:Simple PHP/HTML upload page - no files are saving简单的 PHP/HTML 上传页面 - 没有文件保存
【发布时间】:2015-04-11 19:04:03
【问题描述】:

我是 HTML/PHP 新手,我正在尝试创建一个简单的 php 文件上传页面。 我有这个作为我的 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 = "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;
    }
}
?>

我已将这两个文件上传到我的主机 (000webhost) 中的正确文件夹,但是当我签入我的 /uploads 文件夹时,什么都没有。我已授予所有文件读写和执行权限以尝试调试它 - 稍后我将了解安全性。

任何帮助将不胜感激!

【问题讨论】:

  • 添加 copy() 或更好的 move_uploaded_file() 调用

标签: php html file upload chmod


【解决方案1】:

你必须添加这个sn-p的代码:

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);

之后:

echo "File is an image - " . $check["mime"] . ".";

【讨论】:

    【解决方案2】:

    您需要将上传的文件从上传到的临时目录中移动到您的目标目录中。请参阅move_uploaded_files 的 PHP 文档

    【讨论】:

      【解决方案3】:

      使用 PHP 函数move_uploaded_file(string $filename, string $destination),您可以将文件移动到您想要的路径。

      【讨论】:

        猜你喜欢
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        • 2015-05-04
        • 1970-01-01
        • 2016-01-26
        • 2015-05-13
        相关资源
        最近更新 更多