I’m working on a website, and I need help on one of the scripts that I am using to upload files.
I have a PHP script that allows the user to select a file to be uploaded, and for small files, it seems to work.
However, if the user tries to upload a bigger file, ~3MB, the script won’t upload the file, and it will end up with Upload Error 7, which is UPLOAD_ERR_CANT_WRITE: Failed to write file to disk.
I’ll attach the scripts here for you to look at.
Here is the HTML form:
<form action="uploader.php" method="post" enctype="multipart/form-data">
Select file to upload:<br /><br />
<input type="file" name="fileToUpload" id="fileToUpload"><br /><br />
<input type="submit" value="Upload File" name="submit">
</form>
Here is the PHP script I use for uploading - uploader.php:
Some info about it:
I get the extension of the file uploaded, and create a folder with the name of the extension, and put the file inside. The directory I store the files in is one folder back, in the files folder.
I’ve seen other people’s posts where they suggested trying to change PHP vars like post_max_size, and I have changed them all accordingly - but the script still does not work. I really need help.
<?php
//Upload Folder
$dir = "../files/";
//Uploaded File
$target_file = $dir . basename($_FILES["fileToUpload"]["name"]);
//File's Extension
$extension = pathinfo($target_file, PATHINFO_EXTENSION);
//Filename
$filename = pathinfo($target_file, PATHINFO_BASENAME);
//The folder that is created for the extension
$location = "{$dir}/{$extension}";
//Where the file is placed in the extension folder
$filelocation = "{$location}/{$filename}";
//The fixed file name, with the location set for where it will be placed.
$target_file_2 = $dir . $extension . "/" . basename($_FILES["fileToUpload"]["name"]);
//Link executed after upload...
$link = "http://test.com";
//Debug: Array showing the file's upload status (where I see it hits the error 07)
print_r($_FILES["fileToUpload"]);
//If extension folder exists/doesn't exist
if(file_exists($location))
{
//Uploads the uploaded file, and moves it to the target location: ../files/EXTENSION/filename
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_2);
//header("Location: $link");
}
else
{
//Creates the Extension folder if it does not exist.
mkdir($location);
//Uploads the uploaded file, and moves it to the target location: ../files/EXTENSION/filename
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file_2);
//header("Location: $link");
}
?>
When I try to upload files that are smaller, it is successful, and the debug print shows something like this:
It successfully uploads with 0 errors.
However, if I try to upload a bigger file…
Array ( [name] => testupload.file [type] => [tmp_name] => [error] => 7 [size] => 0 )
It shows that the error I get from uploading is 7, and the size is 0.
I’ve already tried the fixes such as adding “php_value post_max_size 50M” or other fixes to my .htaccess file, nothing seems to work. I can verify that the changes take place, but it still doesn’t fix the upload.
If anyone knows what to do, any help would be sincerely appreciated.