Jump to content

Please help me fix my file upload code?


bubba

Recommended Posts

I've created the html page that calls the uploader.php file. All works well if what I upload passes all the error handling in the uploader file. The problem occurs when an error occurs and the die statement occurs, ending the script. Unfortunately it appears that it ends the entire page including the html that is supposed to show up at the bottom of the page. Is there a way to rewrite what I've done or use something other than die so that the rest of the page will load even when an error occurs during the upload? Here's the code - thanks so much to anyone who can help.

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Welcome!</title>

<style type="text/css">

<!--

body,td,th {

font-family: Trebuchet MS;

}

a:link {

color: #996666;

text-decoration: none;

}

a {

font-weight: bold;

}

a:visited {

text-decoration: none;

color: #996666;

}

a:hover {

text-decoration: underline;

}

.style6 {font-size: 14px}

.style7 {font-size: 10px; font-weight: bold;}

.style8 {font-size: 10px}

a:active {

text-decoration: none;

}

-->

</style>

</head>

 

<body>

  <div align="center"><img src="logotype_2006.jpg" width="477" height="112">

  </div>

  <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">

      <tr>

        <td>

        </td>

      </tr>

</table>

      <table width="400" border="0" align="center" cellpadding="12" cellspacing="0">

          <tr>

            <td valign="top"><div align="center">

              <p class="style6">

 

<?php

  // Configuration - Your Options

      $allowed_filetypes = array('.doc','.pdf','.txt','.rtf','.wpd','.wps'); // These will be the types of file that will pass the validation.

      $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).

      $upload_path = './pending/'; // The place the files will be uploaded to (currently a 'pending' directory).

 

  $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).

  $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

 

  // Check if the filetype is allowed, if not DIE and inform the user.

  if(!in_array($ext,$allowed_filetypes))

      die('Our appologies: The type of file you attempted to upload is not allowed. Only PDFs and word processed documents are accepted for upload. ');

 

  // Now check the filesize, if it is too large then DIE and inform the user.

  if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)

      die('Our apologies: It looks like the file you attempted to upload is too large. Only documents under 500Kb are accepted for upload.');

 

  // Now check if the file already exists.

  if (file_exists($upload_path . $filename))

      die('A file with that name already exists on our server. Please check to be sure that this is not a duplicate submission or rename your file and try again.');

 

  // Upload the file to your specified path.

  if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))

        echo "Success! Your document ".$filename. " has been submitted for review. Once approved, it will appear in our search engine results. Thank you.";

      else

        echo "There was an error - we believe beyond our control - during the file upload.  Please try again.";

 

?>

 

  </p>

              <p>  </p>

            </div></td>

          </tr>

</table>

      <table width="400" border="0" align="center" cellspacing="0" style="padding: 5px;">

          <tr valign="bottom">

            <td width="130" align="center"><p align="center" class="style7"><a href="for_media.htm">for<br>

media</a></p></td>

            <td width="131" align="center"><p align="center" class="style8"><b><a href="index.htm"><img src="typewriter.jpg" width="90" height="60" border="0"></a></b></p>

              <p align="center" class="style8"><b><a href="index.htm">home<br>

              </a></b></p></td>

            <td width="131" align="center"><p align="center" class="style8" style="line-height: 100%;"><b><a href="for_nonprofits.htm">for<br>

nonprofits</a></b></p></td>

          </tr>

</table>

      <br>

    </center>

  </div>

  <div align="center" class="style8">copyright 1999-2007

    </div>

  </div>

</body>

</html>

 

Link to comment
Share on other sites

  die('Our appologies: The type of file you attempted to upload is not allowed. Only PDFs and word processed documents are accepted for upload. ');

to

echo 'Our appologies: The type of file you attempted to upload is not allowed. Only PDFs and word processed documents are accepted for upload. ';

Link to comment
Share on other sites

   if(!in_array($ext,$allowed_filetypes)){
     $count = $count +1;
   }
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize){
     $count.= $count +1;   
}   
   if (file_exists($upload_path . $filename)){
     $count.= $count +1;
   }
if ($count==0) {

   if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename))
		 echo "Success! Your document ".$filename. " has been submitted for review. Once approved, it will appear in our search engine results. Thank you.";
    }
else{
         echo "There was an error - we believe beyond our control - during the file upload.  Please try again.";
  }

 

just change it the way you want

Link to comment
Share on other sites

Here is one way to do it... This script was tested and shouldn't have any parse errors. As far as the $_FILE stuff goes, you're on your own there. I also didn't format the HTML.. at least you'll see how to approach a situation where you might need to send out HTML in different parts of your script (and how to approach the logic of error handling).

 

<?php

// Configuration - Your Options
$allowed_filetypes = array('.doc','.pdf','.txt','.rtf','.wpd','.wps'); // These will be the types of file that will pass the validation.
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB).
$upload_path = './pending/'; // The place the files will be uploaded to (currently a 'pending' directory).

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

// Check if the filetype is allowed, if not DIE and inform the user.
if ( !in_array($ext,$allowed_filetypes) ) {
$err_msg = 'The type of file you attempted to upload is not allowed. Only PDFs and word processed documents are accepted for upload.';
$err_flag = TRUE;
}

// Now check the filesize, if it is too large then DIE and inform the user.
if ( filesize($_FILES['userfile']['tmp_name']) > $max_filesize ) {
$err_msg = 'It looks like the file you attempted to upload is too large. Only documents under 500Kb are accepted for upload.';
$err_flag = TRUE;
}

// Now check if the file already exists.
if ( file_exists($upload_path . $filename) ) {
$err_msg = 'A file with that name already exists on our server. Please check to be sure that this is not a duplicate submission or rename your file and try again.';
$err_flag = TRUE;
}

// If everything cool so far, upload the file to your specified path.
if ( $err_flag ) {
send_header();
echo "There was an error with the file you are attempting to upload. The error is:<br /><br />$err_msg<br /><br />Please try your upload again!";
send_footer();
die();
} else {
send_header();
if ( move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename) ) {
	echo "Success! Your document ".$filename. " has been submitted for review. Once approved, it will appear in our search engine results. Thank you.";
	send_footer();
	die();
} else {
	echo "There was an unkown error - we believe beyond our control - during the file upload.  Please try again.";
	send_footer();
	die();
}
}

function send_header() {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Welcome!</title>
<style type="text/css">
<!--
body,td,th {
  font-family: Trebuchet MS;
}
a:link {
  color: #996666;
  text-decoration: none;
}
a {
  font-weight: bold;
}
a:visited {
  text-decoration: none;
  color: #996666;
}
a:hover {
  text-decoration: underline;
}
.style6 {font-size: 14px}
.style7 {font-size: 10px; font-weight: bold;}
.style8 {font-size: 10px}
a:active {
  text-decoration: none;
}
-->
</style>
</head>

<body>
 <div align="center"><img src="logotype_2006.jpg" width="477" height="112">
 </div>
 <table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
     <tr>
       <td>
       </td>
     </tr>
</table>
     <table width="400" border="0" align="center" cellpadding="12" cellspacing="0">
         <tr>
           <td valign="top"><div align="center">
             <p class="style6">
<?php
}

function send_footer() {
?>
          </p>
             <p>  </p>
           </div></td>
         </tr>
</table>
     <table width="400" border="0" align="center" cellspacing="0" style="padding: 5px;">
         <tr valign="bottom">
           <td width="130" align="center"><p align="center" class="style7"><a href="for_media.htm">for

media[/url]</p></td>
           <td width="131" align="center"><p align="center" class="style8"><a href="index.htm"><img src="typewriter.jpg" width="90" height="60" border="0">[/url]</p>
             <p align="center" class="style8"><a href="index.htm">home

             [/url]</p></td>
           <td width="131" align="center"><p align="center" class="style8" style="line-height: 100%;"><a href="for_nonprofits.htm">for

nonprofits[/url]</p></td>
         </tr>
</table>
   </center>
 </div>
 <div align="center" class="style8">copyright 1999-2007 
   </div>
 </div>
</body>
</html>
<?php

}
?>

 

PhREEEk

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.