Reputation: 27232
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
Upvotes: 2
Views: 19822
Reputation: 451
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.
Upvotes: 0
Reputation: 615
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"
Upvotes: 0
Reputation: 2880
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
Upvotes: 0
Reputation: 11
I solved the problem by using "setenforce 0" to enter permissive mode on SELinux;
[root@localhost tequila]# setenforce 0
Upvotes: 1
Reputation: 4134
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
Upvotes: 4