what is wrong with this upload code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kgc
Forum Newbie
Posts: 1
Joined: Fri May 19, 2006 2:32 pm

what is wrong with this upload code?

Post by kgc »

I am new to php.I have tried the following script and it works well in my computer but when i try it in my webhost it didn't work.I have adjusted all the permissions writable in CHMOD.I could only see the blank page of internet explorer.What may be the problem?Thanks.

index.php

Code: Select all

<html>

<form enctype="multipart/form-data" action="validateFile.php" method="POST"> 
<table> 
<tr> 
   <td><input type="hidden" name="MAX_FILE_SIZE" value="30000"> 
           Upload this file: <input name="userfile" type="file"> 
          <input type="submit" value="Upload File"></td> 
</tr> 
</table> 
</form> 

</html>
validatefile.php

Code: Select all

<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES 

$MAX_FILE_SIZE=50000;
if($_FILES['userfile']['name'] == '') { 
       echo 'You did not select a photo to upload'; 
       } 
elseif($_FILES['userfile']['size'] == 0) { 
   echo 'There appears to be a problem with the photo your are uploading'; 
       } 
elseif($_FILES['userfile']['size'] > $MAX_FILE_SIZE) { 
        echo 'The photo you selected is too large'; 
       } 
elseif(!getimagesize($_FILES['userfile']['tmp_name'])) { 
        echo 'The photo you selected is not a valid image file'; 
       } 
else { 
   $uploaddir = 'images/'; // remember the trailing slash! 
   $uploadfile = $uploaddir. $_FILES['userfile']['name']; 
   if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 
       echo 'Upload file success!'; 
   } 
   else { 
       echo 'There was a problem uploading your file.<br />'; 
       print_r($_FILES); 
   } 
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If your page is blank then you are probably experiencing errors that are being surpressed by your error_reporting level. At the top of your php page add the following code and run it again...

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>
Please take care when using these error reporting settings. I would highly encourage you to not ever use them in a production environment unless you are debugging because if there are errors, a lot of potentially exploitable information is passed to the screen.
Post Reply