user1801060 user1801060 - 1 year ago 78
PHP Question

Print/debug cause of upload error in PHP

I'm a PHP newbie and have written a script to process form uploads. It works for small files(less than 1Mb). However when I try to upload an ~4Mb pdf file it returns an error message. What am I doing wrong?

PS: I ran php_info, got the value of "upload_tmp_dir" and set it to a directory owned by the apache process(www-data)

My client-side code is

<form action="upper.php" method="post" enctype="multipart/form-data">
<input type="file" name="myFile"><br>
<input type="submit" value="Upload">

Upper.php contains

define("UPLOAD_DIR", "/var/www/html/upload/");

if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];

if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "Error is " . $myFile["error"];
//echo "<p>An error occurred.</p>";

$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];

// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"], UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";

// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);

Answer Source

Most probably you have wrong config in you php.ini file.

You need to set

; Maximum allowed size for uploaded files.
upload_max_filesize = 40M

; Must be greater than or equal to upload_max_filesize
post_max_size = 40M

its already described here: PHP change the maximum upload file size

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download