【问题标题】:upload.php is not working on my serverupload.php 在我的服务器上不起作用
【发布时间】: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 从您的代码中丢失了一些将文件上传到服务器的代码,从您上面的代码中,您只是在检查是否可以上传。

标签: php html upload


【解决方案1】:

您的代码中没有任何函数可以将上传的文件复制到目标目录中。

你必须添加这个:

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

参加本文中的问题作者评论,我更新了代码以创建文件夹,如果它不存在。

所以你的代码应该是这样的:

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) {

       // Check if the upload directory exists and create if necessary
       if (!is_dir($target_dir)) {
           mkdir($target_dir);
       }

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

}

【讨论】:

  • 感谢您的回复,请查看上面的编辑以查看新问题
  • @R.Jones 我更正了代码,它有语法错误。如果文件夹不存在,我会创建它;)
猜你喜欢
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 2016-06-03
  • 1970-01-01
相关资源
最近更新 更多