/    Sign up×
Community /Pin to ProfileBookmark

Only allowing Jpg’s to be uploaded !!

What is the code to allow jpg to be uploaded through my site.

Ive used this tutorial in learning how to up load files but it does include this in it.

[url]http://www.phpeasystep.com/phptu/1.html[/url]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Feb 13.2007 — First, in your form, you should use the "accept" with a comma-separated list of the MIME types that you want to accept for upload.[code=php]<input type="file" name="upfile" accept="mime1,mime2, etc">. In your case, the MIME type would be: "image/jpeg".

You shouldn't rely on that only, however. You should check on the received MIME types as you post-process the uploaded file(s). For more on that, see: http://www.webdeveloper.com/forum/showthread.php?t=101466
Copy linkTweet thisAlerts:
@popcopauthorFeb 13.2007 — how do u add that to this code then?

[code=php]
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
echo "<img src="$path" width="150" height="150">";
}
else
{
echo "Error";
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@NightShift58Feb 13.2007 — Before you do the copy() part, you need to ask:[code=php]if ($HTTP_POST_FILES['ufile']['type'] == "image/jpeg") {
copy(...)
} else {
echo "You tried to sneak one in...";
}[/code]
Copy linkTweet thisAlerts:
@popcopauthorFeb 13.2007 — is it better to upload the image to the server or to the database?

or is there much difference
Copy linkTweet thisAlerts:
@NightShift58Feb 13.2007 — Personally, I leave them on the server. I'm more flexible that way. but it's a personal choice.
Copy linkTweet thisAlerts:
@bokehFeb 13.2007 — To check the mime type of an image use [I]getimagesize()[/I] which checks the file headers. If you are extra paranoid you could even use [I]imagecreatefromjpeg()[/I] to see whether the image can be opened. Do this before considering moving the image.
×

Success!

Help @popcop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...