PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 24, 2014 9:56 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Aug 21, 2012 8:22 am 
Offline
Forum Newbie

Joined: Sat Aug 18, 2012 7:49 am
Posts: 2
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 ]
<?php
// Checks if the form was submitted
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// Checks if a file was uploaded without errors
if(isset($_FILES['photo'])
&& 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
imagedestroy($image);
} else {
print_R($_FILES);
}
} else {
echo "No photo uploaded!" ;
}
} else {
// If the form was not submitted, displays the form HTML
?>
<form action="form.php" method="post"
enctype="multipart/form-data">
<label for="photo">User Photo:</label>
<input type="file" name="photo" />
<input type="submit" value="Upload a Photo" />
</form>
<?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:
Quote:
Array ( [photo] => Array ( [name] => Untitled.jpg [type] => image/pjpeg [tmp_name] => C:\wamp\tmp\phpC401.tmp [error] => 0 [size] => 357507 ) )


Top
 Profile  
 
PostPosted: Tue Aug 21, 2012 10:36 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5647
Location: WA, USA
Code:
[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.


Top
 Profile  
 
PostPosted: Wed Aug 22, 2012 5:26 pm 
Offline
Tranquility In Moderation
User avatar

Joined: Sun Feb 06, 2005 8:18 pm
Posts: 4990
Location: Indiana
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().


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group