skelto skelto - 5 months ago 38
Ajax Question

File too large to upload to server even when php.ini file altered

I have been unable to upload a (.OBJ) file to the server and get an 'Error 1' meaning that


The uploaded file exceeds the upload_max_filesize directive in php.ini


I got this information from this page - http://php.net/manual/en/features.file-upload.errors.php

I then went to the php.ini file and changed both the
upload_max_filesize
and the
post_max_size
to 10000M.

I still get an 'Error 1' when I try to upload the file even though it is only 3MB. I have also restarted the server so that changes I have made to the php.ini file can come into effect. I know that my code works for smaller files as I have been able to send a small TXT file that contains the word "Test".

Here is the form.php file that contains the PHP code for handling the sent file on the server side and HTML code to create the file selection and upload functionality.

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

ini_set('max_execution_time', 3000000);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
var_dump($_FILES);
echo "<br/><b>File Name:</b> " . $_FILES["file"]["name"] . "<br>";
echo "<b>Type:</b> " . $_FILES["file"]["type"] . "<br>";
echo "<b>Size:</b> " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "<b>Temp File Name:</b> " . $_FILES["file"]["tmp_name"] . "<br>";
echo "<b>Error:</b> " . $_FILES["file"]["error"] . "<br>";

$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = "upload/" . $_FILES['file']['name']; // Target path where file is to be stored

move_uploaded_file($sourcePath, $targetPath);

exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
<body>
<form action="form.php" method="post" enctype="multipart/form-data">
<label>File</label>
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
<div></div>
</body>
<script>

$(function () {
$('form').on('submit', function (e) {
e.preventDefault();

$.ajax({
url: this.action,
type: this.method,
data: new FormData(this),
processData: false,
contentType: false,
success: function (res) {
$('div').html(res);
}
});
});
});

</script>
</html>


To sum up my question is, why can I not upload the desired file even though I have changed the appropriate variables of the php.ini file?

Answer

You have to restart you Webserver:

service apache restart #If you have Apache
service nginx restart #If you have NGINX

If your running php-fpm:

service php-fpm restart

Do you have multiple php.ini's? Look out for:

/etc/php/php-fpm
/etc/php/php-cli
/etc/php/php-cgi

Under some circumstances you also have to allow bigger file uploads on your Webserver.

NGINX:

client_max_body_size 10000M;
Comments