move_uploaded_file()无法移动上传的文件

时间:2018-08-15 11:32:31

标签: php

上传文件时出现这两个错误-

ERROR - 2018-08-15 12:21:25 --> Severity: Warning --> move_uploaded_file(/var/www/myproject/uploads/temp_userscsv/1534332085---users.csv): failed to open stream: No such file or directory /var/www/myproject/admin/Users.php 675
ERROR - 2018-08-15 12:21:25 --> Severity: Warning --> move_uploaded_file(): Unable to move '/tmp/phpEWF5d4' to '/var/www/myproject/uploads/temp_userscsv/1534332085---users.csv' /var/www/myproject/admin/Users.php 675

为消除权限问题,我测试了在/ tmp中创建文本文件

$handle = fopen("/tmp/test.txt", "x");
if ($handle) echo "Success!";
else print_r(error_get_last());


$fp = fopen('/tmp/test.txt', 'w');
fwrite($fp, '100');
fwrite($fp, '230');
fclose($fp);


$filename = "/tmp/test.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

print $contents;

它工作正常,我也可以在终端的/tmp文件夹中查看文件。

我不知道为什么上传的文件有问题?有什么想法吗?

我也已经进行了这些错误检查,这些错误检查没有注册任何错误-

        switch ($_FILES['userscsv']['error']) {
            case UPLOAD_ERR_OK: //0
                $errors[] = 'There is no error, the file uploaded with success.';
                break;
            case UPLOAD_ERR_INI_SIZE: //1
                $errors[] = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
                break;
            case UPLOAD_ERR_FORM_SIZE: //2
                $errors[] = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
                break;
            case UPLOAD_ERR_PARTIAL: //3
                $errors[] = 'The uploaded file was only partially uploaded.';
                break;
            case UPLOAD_ERR_NO_FILE: //4
                $errors[] = 'No file was uploaded.';
                break;
            case UPLOAD_ERR_NO_TMP_DIR: //6
                $errors[] = 'Missing a temporary folder.';//Introduced in PHP 4.3.10 and PHP 5.0.3.
                break;
            case UPLOAD_ERR_CANT_WRITE: //7
                $errors[] = 'Failed to write file to disk.'; //Introduced in PHP 5.1.0.
                break;
            case UPLOAD_ERR_EXTENSION: //8
                $errors[] = 'File upload stopped by extension.'; //Introduced in PHP 5.2.0.
                break;
            default:
                $errors[] = 'Unknown upload error';
                break;
        }

最后这是我的675行

$result_move_uploaded = move_uploaded_file($_FILES['userscsv']['tmp_name'], '/var/www/myproject/uploads/temp_userscsv/1534332085---users.csv');

更新

这是html

<form action="" method="post" enctype="multipart/form-data" class="form-horizontal">
    <div class="form-body">
        <div style="padding-left:20px;">
            <input type="hidden" name="postaction" value="uploadfile" />
            <input type="hidden" name="ttoken" value="3525235" />

            <div class="form-group">
                <label for="uploadcsv">Upload File:</label>
                <input type="file" name="userscsv" id="uploadcsv" class="btn btn-default btn-file" />
            </div>
        </div>
    </div>
    <div class="form-actions">
        <div class="row">
            <div class="col-md-offset-4 col-md-8">
                <a href="#" title="Cancel and go back" onclick="history.back(-1); return false;"  class="btn default" ><i class="fa fa-chevron-left" aria-hidden="true"></i> Cancel and go back</a>                                
                <button class="btn btn-success" type="submit">Upload</button>
            </div>
        </div>
    </div>
</form>

2 个答案:

答案 0 :(得分:1)

尝试使用完整路径move_uploaded_file($_FILES['userscsv']['tmp_name'],uploads/temp_userscsv/1534332085---users.csv)的短路径

答案 1 :(得分:0)

尝试一下:

在目标文件的路径之前使用$_SERVER['DOCUMENT_ROOT'],以确保绝对路径正确。

更改此内容:

$result_move_uploaded = move_uploaded_file($_FILES['userscsv']['tmp_name'], '/var/www/myproject/uploads/temp_userscsv/1534332085---users.csv');

对此:

$result_move_uploaded = move_uploaded_file($_FILES['userscsv']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/var/www/myproject/uploads/temp_userscsv/1534332085---users.csv');

希望我进一步推动了你。