上传文件到网络服务器错误 6 php

问题描述 投票:0回答:6

我正在尝试从 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.";
        }
    }
    }
    ?>

我已经搜索过这个问题并找到了解决方案这里。但理解不太到位。

我还授予了 /uploads 文件夹完全权限

777
。 任何帮助将不胜感激。谢谢

php file-upload upload php-ini
6个回答
4
投票
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


1
投票

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

[root@localhost tequila]# setenforce 0

0
投票

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

在 ubuntu 中您可以使用以下命令来重新启动 apache 服务:

sudo /etc/init.d/apache2 restart

0
投票

编辑您的 php.ini 文件,并更新以下更改并重新启动 apache 或 iis

像下面这样-

error_log = "C:\Windows ew_folder\php-5.6.1_errors.log"

upload_tmp_dir = "C:\Windows ew_文件夹”


0
投票

使用 PHP 5.4 设置 Plesk

对我来说,

upload_tmp_dir
没有任何价值。我尝试设置
/tmp
,即使有 777/1777 权限,也不起作用。

因此,我在 php.ini 的值

tmp
中提到的文件夹中创建了一个新目录
open_basedir
。对我来说是
/var/www/vhosts/xxxxxx.xxx/
,然后我将其设置为
upload_tmp_dir='/var/www/vhosts/xxxxxx.xxx/tmp'

我使用 Plesk 仪表板设置 PHP 配置 ->

Additional Directives

并且成功了。

快乐编码。


-1
投票

我也有同样的问题。我已经检查了以上所有设置,但没有找到解决方案。我只有以下错误:

Error in Web consol

© www.soinside.com 2019 - 2024. All rights reserved.