Why this code dont work well?

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

Why this code dont work well?

Postby yarin1312 » Tue Aug 21, 2012 8:22 am

i wrote a code in php that give you to upload a picture to the website, and now this is the code:
Syntax: [ Download ] [ Hide ]
// Checks if the form was submitted
// Checks if a file was uploaded without errors
&& is_uploaded_file($_FILES['photo']['tmp_name'])
&& $_FILES['photo']['error']==UPLOAD_ERR_OK) {
// Checks if the file is a JPG image
if($_FILES['photo']['type']=='image/jpeg') {
$tmp_img = $_FILES['photo']['tmp_name'];
// Creates an image resource
$image = imagecreatefromjpeg($tmp_img);
// Tells the browser what type of file
header('Content-Type: image/jpeg');
// Outputs the file to the browser
imagejpeg($image, '', 90);
// Frees the memory used for the file
} else {
} else {
echo "No photo uploaded!" ;
} else {
// If the form was not submitted, displays the form HTML
<form action="form.php" method="post"
<label for="photo">User Photo:</label>
<input type="file" name="photo" />
<input type="submit" value="Upload a Photo" />
<?php } // End else statement ?>

this code is from the PHP for absolute beginner of apress and every time i try to upload a jpg picture, and i tried four, its give me this:
Array ( [photo] => Array ( [name] => Untitled.jpg [type] => image/pjpeg [tmp_name] => C:\wamp\tmp\phpC401.tmp [error] => 0 [size] => 357507 ) )
Forum Newbie
Posts: 2
Joined: Sat Aug 18, 2012 7:49 am

Re: Why this code dont work well?

Postby requinix » Tue Aug 21, 2012 10:36 am

Code: Select all
[type] => image/pjpeg

Compare that with what your code expects.

Don't use the [type] to determine the type of file. It's provided by the browser which means it could be absolutely anything: right, different, or even completely wrong. Determine the file type yourself. Fortunately that's easy to do if you use getimagesize.
User avatar
Spammer :|
Posts: 5604
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Why this code dont work well?

Postby s.dot » Wed Aug 22, 2012 5:26 pm

It appears that it's reaching the print_r() section of the code and is working as expected.
I believe IE uses the img/pjpeg mime time.
The post above me is correct, use getimagesize().
User avatar
Tranquility In Moderation
Posts: 4990
Joined: Sun Feb 06, 2005 8:18 pm
Location: Indiana

Return to PHP - Code

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 14 guests