user2133499
user2133499

Reputation: 13

Image upload PHP code not working if images are too big

I have the following two PHP code to upload files to my website. The first one to create directories. The second is to upload the images.

This is my PHP code to create the directories:

<?php
$foldername = $_POST["foldername"]; 
mkdir("uploads", 0777); // Create Folder
mkdir("uploads/input", 0777); // Create Folder
mkdir("uploads/output", 0777); // Create Folder
mkdir("uploads/output/".$foldername, 0777); // Create Folder
mkdir("uploads/output/".$foldername."/modified", 0777); // Create Folder
mkdir("uploads/output/".$foldername."/originals", 0777); // Create Folder
mkdir("uploads/output/".$foldername."/thumbnails", 0777); // Create Folder
?>

This is the one to upload files.

  $success = 0;
  $fail = 0;
  $uploaddir = 'uploads/input/';
  for ($i=0;$i<10;$i++)
  {
   if($_FILES['userfile']['name'][$i])
   {
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
    $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
    if (preg_match("/(jpg|gif|png|bmp)/",$ext))
    {
     if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile)) 
     {
      $success++;
     } 
     else 
     {
     echo "Error Uploading the file. Retry after sometime.\n";
     $fail++;
     }
    }
    else
    {
     $fail++;
    }
   }
  }
  echo "<br> Number of files Uploaded: ".$success;
  echo "<br> Number of files Failed: ".$fail. "<br><br>";
?>

Both PHP codes are executed through another PHP code. Both codes work fine. Unfortunately if the file sizes are huge the first and the second code do not work. I think its because the first code doesn't work for large files then the second doesn't work.

I think because it takes too long to upload the images that the code doesn't work. What can I do please?

I get the following in the error_log PHP Warning: POST Content-Length of 12304331 bytes exceeds the limit of 10485760 bytes in Unknown on line 0

Upvotes: 1

Views: 1950

Answers (2)

Anthony Sterling
Anthony Sterling

Reputation: 2441

You will need to configure the upload_max_filesize, post_max_size, and memory_limit directives.

Upvotes: 0

Robert Owen
Robert Owen

Reputation: 930

Your current PHP INI is set to a max upload size of 10mb.

Increase the size of the upload limit in your php ini or use:

ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');

Upvotes: 2

Related Questions