We help IT Professionals succeed at work.
Get Started

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

Webspeeder
Webspeeder asked
on
296 Views
Last Modified: 2013-12-12
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
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE