Error Upload Image...

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
teckyan888
Forum Commoner
Posts: 40
Joined: Tue May 11, 2004 10:46 am

Error Upload Image...

Post by teckyan888 »

Code: Select all

<?php
<form enctype="multipart/form-data" action="image.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> 

<?php 

// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES 
foreach($_FILES as $value) { 
foreach($value as $k => $v) { 
echo $k.' => '.$v.'<br>'; 
} 
} 



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 = '/path/to/your/image/files/'; // 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); 
} 
} 

?> 


?>
It display the message:
name => Motorola380.jpg
type => image/pjpeg
tmp_name => C:\WINDOWS\TEMP\php2B.tmp
error => 0
size => 14804
The photo you selected is too large

But i check already...My size is not large than my MAX_FILE_SIZE...But how come it is still cannot upload my image?Anyone who know what is the problem??
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

$MAX_FILE_SIZE is not a known global - i.e. just because it is in your form, it doesn't mean PHP will turn it into a variable for you. Set it to something at the start of your script.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

To identify the problem instead of

Code: Select all

//.....
elseif($_FILES['userfile']['size'] > $MAX_FILE_SIZE) {
echo 'The photo you selected is too large';
} 
//.....
use

Code: Select all

//.....
elseif($_FILES['userfile']['size'] > $MAX_FILE_SIZE) {
echo "The photo you selected is too large.<br />\nSize of your file:{$_FILES['userfile']['size']}<br />\nMaximum allowed size is: $MAX_FILE_SIZE<br />\n";
} 
//.....
Post Reply