NoNice
NoNice

Reputation: 411

move_uploaded_file() Unable to move uploaded file

I get these two errors when I upload a file -

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

To eliminate permissions issue, I tested creating a text file in /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;

It works fine and I can view the file in the /tmp folder on terminal too.

I can't figure out why it's having issues with uploaded files? Any ideas?

I have got these error checks in place too which don't register any errors -

        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;
        }

Finally this is my line 675

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

UPDATE

Here's the 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>

Upvotes: 0

Views: 267

Answers (2)

Jora
Jora

Reputation: 21

Try to short path of full path move_uploaded_file($_FILES['userscsv']['tmp_name'],uploads/temp_userscsv/1534332085---users.csv)

Upvotes: 1

ThS
ThS

Reputation: 4773

Try this:

Using $_SERVER['DOCUMENT_ROOT'] prepended to the path of the destination file ensure that the absolute path is correct.

change this:

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

into this:

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

Hope I pushed you further.

Upvotes: 0

Related Questions