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 asked
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?
ini_set('display_errors', 1);

# Determines which file extensions will be allowed for upload 
$allowedExtensions = array("txt","csv","htm","html","xml","zip", 
foreach ($_FILES as $file) { 
  if ($file['tmp_name'] > '') { 
    if (!in_array(end(explode(".", 
          $allowedExtensions)) { 
     die($file['name'].' is an invalid file type!<br/>'. 
      '<a href="javascript:history.go(-1);">'. 
      '&lt;&lt Go Back</a>'); 



    $target = "<webroot1>".$newdirectory;
    $target = "<webroot2>";

#$target = $target . basename( $_FILES['uploaded']['name']);
$basename =basename($_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*/

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

//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 
    $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;
        $target = $target.$newfile1;
    if(move_uploaded_file($temp_name, $target)) 
       if ($zipflag=='yes')
           $zip = new ZipArchive;
           if ($zip->open($target) === TRUE) 
               $files = array();
               if ($handle = opendir($zipdirectory)) {
                   while (false !== ($file = readdir($handle))) {
                       if ($file != "." && $file != "..")
               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' />"; 
               echo "<br />"."Sorry, there was a problem uploading your zip file.".
                    "<input type='hidden' name='uploadstat' id='uploadstat' value='no' />";
           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' />"; 
    /******* 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' />";


Open in new window

Watch Question
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