We help IT Professionals succeed at work.

I have an error while uploading a PDF file with PHP but don't know what it is

Webspeeder
Webspeeder asked
on
Hey all.

I have a PHP program that fails when trying to upload a file. Of the three PDF files in question, 2 are uploaded fine but the third fails.

The first two are 1.5MB, the third is 2.8MB. The size limit is set to 5MB.

How would I go about finding what error is being generated? Below is the code in question. The $temp_name has a value when the file succeeds but is blank when the file fails, but how do I know why it is blank?
<?php
 
ini_set('display_errors', 1);
error_reporting(E_ALL);

# Determines which file extensions will be allowed for upload 
$allowedExtensions = array("txt","csv","htm","html","xml","zip", 
  "css","doc","xls","docx","xlsx","rtf","ppt","pdf","swf","flv","avi", 
  "wmv","mov","jpg","jpeg","gif","png","tif","psd"); 
foreach ($_FILES as $file) { 
  if ($file['tmp_name'] > '') { 
    if (!in_array(end(explode(".", 
          strtolower($file['name']))), 
          $allowedExtensions)) { 
     die($file['name'].' is an invalid file type!<br/>'. 
      '<a href="javascript:history.go(-1);">'. 
      '&lt;&lt Go Back</a>'); 
    } 
  } 
} 

$newdirectory=$_POST["imgdir"];
$imgname=$_POST["imgname"];

$zipdirectory=$_POST["extractdir"].$_POST["extractname"];
$zipflag=$_POST["zipflag"];
$imgflag=$_POST["imgflag"];

if($newdirectory!="")
    $target = "<webroot1>".$newdirectory;
else
    $target = "<webroot2>";


#$target = $target . basename( $_FILES['uploaded']['name']);
$basename =basename($_FILES['uploaded']['name']);
$file_name=($_FILES['uploaded']['name']);
$file_name=(strtolower($_FILES['uploaded']['name']));/* converts the file name you see for confirmation to lower case */
$file_name=preg_replace('/[^a-zA-Z0-9\.]/', '', $file_name);/* removes all unwanted characters from the file name you see for confirmation*/
$temp_name=($_FILES['uploaded']['tmp_name']);
$file_size=($_FILES['uploaded']['size']);
$file_type=($_FILES['uploaded']['type']);
$ok=1; 

//This is our size condition 
if ($file_size > 5000000) 
{ 
  echo "Your file is too large.<br>".$file_size."<br />"; 
  $ok=0; 
}  

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
  Echo "Sorry your file was not uploaded because of an error";
} 

//If everything is ok we try to upload it 
else 
{ 
    $newfile = strtolower(basename( $_FILES['uploaded']['name'])); /* converts the file name to lower case */
    $newfile1 = preg_replace('/[^a-zA-Z0-9\.]/', '', $newfile); /* removes all unwanted characters */
    if ($imgflag=='yes')
        $target = $target.$imgname;
    else
        $target = $target.$newfile1;
    if(move_uploaded_file($temp_name, $target)) 
    {    
       chmod($target,0644);
       if ($zipflag=='yes')
       {
           $zip = new ZipArchive;
           if ($zip->open($target) === TRUE) 
           {
               $zip->extractTo($zipdirectory);
               $zip->close();
               chmod($zipdirectory,0777);
               $files = array();
               if ($handle = opendir($zipdirectory)) {
                   while (false !== ($file = readdir($handle))) {
                       if ($file != "." && $file != "..")
                           chmod($zipdirectory."/".$file,0777);
                   }
                   closedir($handle);
               }
               echo "The zip file " .$target.  " has been uploaded."."<br />".
               "<input type='button' id='btncontinue' value='CLICK HERE TO COMPLETE UPLOAD' />".
               "<input type='hidden' name='uploadstat' id='uploadstat' value='yes' />"; 
           } 
           else 
           {
               echo "<br />"."Sorry, there was a problem uploading your zip file.".
                    "<input type='hidden' name='uploadstat' id='uploadstat' value='no' />";
           }
       }
       else
       {
           echo "The file " .$target.  " has been uploaded."."<br />"." SRC=".$temp_name."<br />".
           "<input type='button' id='btncontinue' value='CLICK HERE TO COMPLETE UPLOAD' />".
           "<input type='hidden' name='uploadfilename' id='uploadfilename' value='".$newfile1."' />".
           "<input type='hidden' name='uploadstat' id='uploadstat' value='yes' />"; 
       }
    }
    else 
    { 
    /******* THIS CODE IS HIT WHEN FILE FAILS ******/
       echo "<br />"."Sorry, there was a problem uploading your file. SRC=".$temp_name."<br />"." TARGET=".$target.
            "<input type='hidden' name='uploadstat' id='uploadstat' value='no' />";
    }  
} 

connection_aborted();
?>

Open in new window

Comment
Watch Question

Author

Commented:
The $temp_name is blank, but the $target is populated as expected.
I know you have set an upload file size of 5000000 in your code, but PHP has a built in limit for this. Execute this script

<?php
phpinfo();
?>

and scan for the following settings

upload_max_filesize
post_max_size

What are they set to? A value like 2M is 2MB.

Author

Commented:
pos_max_size = 8M
upload_max_filesize = 2m

Well now, how do i go about changing that? is it involved? I'm not the one in charge of the installation.

Author

Commented:
I googled it. Is this the correct way to go about it?

edit php.ini and change the value of upload_max_filesize
You can try using either .htaccess or ini_set, but if they fail then you'll need to get whoever can edit php.ini to change it. Try adding

ini_set('upload_max_filesize', 8000000);

to the start of your script. Failing that try creating a file in the same folder as your upload script called .htaccess (note the leading dot) and put this line in it

php_value upload_max_filesize 8M

If you can edit php.ini and change the value then that is the best way. Look for a line like

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

and change it to

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

Author

Commented:
Thanks for your help. I appreciate the quick response.
You're welcome