【问题标题】:upload file to web server error 6 php将文件上传到 Web 服务器错误 6 php
【发布时间】:2015-01-21 20:27:27
【问题描述】:

我正在尝试从 php 表单上传文件。它无法在 linux 服务器上运行。我希望它从文件所在的位置将其移动到子目录“/uploads”。

当我通过echo$_FILES["file_upload"]['error'];执行页面获取错误号时,出现以下错误。

返回:

Upload failed with error code 6

有谁知道这个错误代表什么?

表格代码:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select File to upload:
    <input type="file" name="file_upload" id="file_upload">
    <input type="submit" value="Upload File" name="submit">
</form>

</body>
</html>

上传.php:

<?php

    if(isset($_POST['submit']))
    {
        if($_FILES['file_upload']['name'] != "" )
    {
            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["file_upload"]["name"]);
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            $uploadOk = 1;
    }
    else
    {
        die("No file specified!");
    }

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    // Check file size
    if ($_FILES["file_upload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }

    // Allow certain file formats
    if($imageFileType != "txt" && $imageFileType != "xls" && $imageFileType != "xlsx")
     {
        echo "Sorry, only TXT, XLS, XLSX files are allowed.";
        $uploadOk = 0;
    }

    if ($_FILES["file_upload"]['error'] !== UPLOAD_ERR_OK) {
       die("Upload failed with error code " . $_FILES['file_upload']['error']);
    }
    // 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["file_upload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file_upload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    }
    ?>

我已经搜索过这个问题并找到了解决方案Here。但不能正确理解。

我还授予了 /uploads 文件夹的完全权限 777。 任何帮助将不胜感激。谢谢

【问题讨论】:

  • 你应该先搜索问题。可能重复stackoverflow.com/questions/8719244/…
  • @sgt,我已经在搜索问题但不明白这个帖子给出的解决方案。
  • 它在那里。可能你不知道设置。然后问正确的问题。

标签: php file-upload upload php-ini


【解决方案1】:
UPLOAD_ERR_NO_TMP_DIR

Value: 6; Missing a temporary folder. Introduced in PHP 5.0.3.

http://php.net/manual/en/features.file-upload.errors.php

修正你的设置..

http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir

【讨论】:

    【解决方案2】:

    我通过在 SELinux 上使用“setenforce 0”进入许可模式解决了这个问题;

    [root@localhost tequila]# setenforce 0
    

    【讨论】:

    • 这是 CentOS Linux 版本 7.7.1908 的修复程序。没有这个,php.ini 中的 sys_temp_dir 会被忽略。赞成。
    【解决方案3】:

    如果文件上传工作正常并且 /tmp 文件夹正在用于上传文件,则只需重新启动 apache 服务即可解决此问题:

    在 ubuntu 中你可以使用以下命令重启 apache 服务:

    sudo /etc/init.d/apache2 restart
    

    【讨论】:

      猜你喜欢
      • 2012-02-21
      • 1970-01-01
      • 2011-07-02
      • 2017-12-09
      • 2014-04-04
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多