image upload problems..

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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

image upload problems..

Post by hob_goblin »

Im trying to let a user upload an image, but I only want them uploading images that are 50x50 or smaller, and that are under 8k.

here is what i tried:

Code: Select all

<?

if($HTTP_POST_VARS&#1111;'action'] == "go")&#123;
$uploaddir = "/usrimages";
$final_filename = "blah.jpg";

clearstatcache();


 if(filesize($imgfile) < 8192 && imagesx($imgfile) <= 50 && imagesy($imgfile) <= 50)&#123;
   $newfile = $uploaddir . "/" . $final_filename;
   copy($imgfile, $newfile);
   &#125; else &#123;
	echo "File too big.";
	&#125;
&#125; else &#123;
?>
<form method="post" action="<?=$PHP_SELF;?>">
 <input type="file" name="imgfile">
 <input type="hidden" name="action" value="go">
 <input type="submit">
</form>
<?
&#125;
?>
Warning: stat failed for F:\\3.gif (errno=2 - No such file or directory) in /home/vexdev/public_html/image.php on line 10

Warning: imagesx(): supplied argument is not a valid Image resource in /home/vexdev/public_html/image.php on line 10

Warning: imagesy(): supplied argument is not a valid Image resource in /home/vexdev/public_html/image.php on line 10

Warning: Unable to open 'F:\\3.gif' for reading: No such file or directory in /home/vexdev/public_html/image.php on line 12

is what i get... i realize this is probably very wrong.. but any suggestions on how i would approach this task?
Tiimmy
Forum Commoner
Posts: 38
Joined: Sat Apr 27, 2002 1:56 am
Location: Australia
Contact:

Post by Tiimmy »

Are you certain that the file you're attempting to upload exists?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

well, it exists on my computer. it's a <input> with the type as file, so i browsed and selected it right then and there...
Tiimmy
Forum Commoner
Posts: 38
Joined: Sat Apr 27, 2002 1:56 am
Location: Australia
Contact:

Post by Tiimmy »

Try calling $imgfile with $_POST['imgfile'] if you've got register_globals off, and see if that helps. :?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

nope, i have an older version of php on the server, and register globals on.. i just use $HTTP_POST_VARS and the like because im trying not to have bad habits...
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

See what you get with:

Code: Select all

echo $HTTP_POST_FILES&#1111;'imgfile']&#1111;'name']."<br>";
echo $HTTP_POST_FILES&#1111;'imgfile']&#1111;'size'];
If the size is 0, then something went wrong with PHP getting the file. You also might want to try putting

Code: Select all

<input type="hidden" name="MAX_FILE_SIZE" value="max_size_in_bytes">
before your <input type="file"> line. I heard that some browsers won't send the file if this field isn't specified
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

ok, i got the upload to work, and check the size...

Code: Select all

<?

if($HTTP_POST_VARS&#1111;'action'] == "go")&#123;

foreach($HTTP_POST_FILES&#1111;'imgfile'] as $key => $value)&#123;
echo $key." - - - ".$value."\n<br>";
&#125;

$uploaddir = "/home/vexdev/public_html/usrimages";
$final_filename = $HTTP_POST_FILES&#1111;'imgfile']&#1111;'name'];

if($HTTP_POST_FILES&#1111;'imgfile']&#1111;'size'] < 8192
 && imagesx($HTTP_POST_FILES&#1111;'imgfile']&#1111;'tmp_name']) <= 50
 && imagesy($HTTP_POST_FILES&#1111;'imgfile']&#1111;'tmp_name']) <= 50)&#123; 
   $newfile = $uploaddir . "/" . $final_filename; 
   copy($HTTP_POST_FILES&#1111;'imgfile']&#1111;'tmp_name'], $newfile); 
   &#125; else &#123; 
   echo "File too big."; 
   &#125; 

&#125; else &#123;

?>
<form method="post" action="<?=$PHP_SELF;?>"
enctype="multipart/form-data">
 <input type="hidden" name="MAX_FILE_SIZE" value="8192">
 <input type="file" name="imgfile">
 <input type="hidden" name="action" value="go">
 <input type="submit">
</form>
<?
&#125;
?>
it had to do with the "enctype" in the form...

but, it still wont check the images height and width... how do i do that? :(
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post by samscripts »

Hi, the imagesx and imagesy take a gd image resource as the argument, not a filename.

see http://www.php.net/ImageCreateFromjpeg

it might be easier to just use:

Code: Select all

$im = getimagesize($filename);
$width = $im&#1111;0];
$height = $im&#1111;1];
see http://www.php.net/getimagesize

hope this helps, sam
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

what i ended up doing was using createimagefromjpeg ... but then i got <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> off because i wanted gif's and createimagefromgif wouldn't work and went and used getimagsize() and grabbed the size of the tmp_name ... so, it all worked out in the end...
Post Reply