2

I am trying to upload a file from a php form.It wont work on linux server. I want it to move it to a subdir '/uploads' from where the file is.

I get the below error when executing the page by echo the $_FILES["file_upload"]['error']; to get the error number.

It returns :

Upload failed with error code 6

Does anyone know what this error stands for?

Form Code :

<!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>

upload.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.";
        }
    }
    }
    ?>

I already search for this problem and find the solution Here. But not undestand properly.

I also given full permission 777 to /uploads folder. Any help will be greatly appreciated. Thanks

3
  • You should search first for the problem.Possible duplicate stackoverflow.com/questions/8719244/… Commented Nov 24, 2014 at 7:15
  • @sgt, I already search for the problem but not understand the solution given by this post. Commented Nov 24, 2014 at 7:19
  • it is there.Probably you dont know the settings then. Then ask the proper question. Commented Nov 24, 2014 at 7:23

5 Answers 5

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

Fix your settings..

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

Sign up to request clarification or add additional context in comments.

Comments

1

I solved the problem by using "setenforce 0" to enter permissive mode on SELinux;

[root@localhost tequila]# setenforce 0

1 Comment

This was the fix for CentOS Linux release 7.7.1908. sys_temp_dir in php.ini was ignored without this. Upvoted.
0

In case if file upload working previously fine and /tmp folder is in use for uploading files then just restart apache services to fix this problem:

In ubuntu you can use following command to restart apache services :

sudo /etc/init.d/apache2 restart

Comments

0

Edit your php.ini file , and update the below changes and restart apache or iis

like below -

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

upload_tmp_dir = "C:\Windows\new_folder"

Comments

0

Plesk setup with PHP 5.4

For me, it was upload_tmp_dir having no value. I tried to set /tmp and that too didn't work even if it had 777/1777 permission.

So, I created a new directory tmp in the folder which was mentioned in the value open_basedir of php.ini. For me it was /var/www/vhosts/xxxxxx.xxx/ and then I set it up as upload_tmp_dir='/var/www/vhosts/xxxxxx.xxx/tmp'.

I used Plesk dashboard to set PHP Configuration -> Additional Directives.

And it worked.

Happy coding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.