By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

User uploading pictures

P: n/a
Hi All,

I have a web site I am developing, and have a question. I would like
members to be able to upload pictures.

Do you think they should be saved as individual files or should they be
put in an MySQL database? Which would you recommend, and do you have any
sample code for accomplishing this?

Ciao . . . C.Joseph

And on the seventh day God said,
"I will rest . . . Murphy take over."
May 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Here ya go. This is the script that I made for one of my sites. The
form takes the userid, and if the image gets uploaded ok it gets
renamed to useridu.jpg (100u.jpg) I only allow jpg's for this script.
It then gets inserted in an images table as unapproved (0). After I
approve the images the image gets renamed to userid.jpg (100.jpg) and
the users profile gets updated with the new picture.
Upload form

<?php

echo "Upload an image<br><br>";
echo "
<form action=\"uploader.php\" method=\"post\"
enctype=\"multipart/form-data\">

<input type=\"file\" name=\"file\" size=\"50\">
<br/>
<input type=\"submit\" value=\"Upload File\">
<input type='hidden' name='userid' value='$userid'>
</form>[ <a href=users.php class=\"menulink\">Back</a> ]<br><br>";

?>

uploader.php

<?php
global $userid;
if($file_name !="")
{
$image_type = strstr($file_name, '.');

if($image_type == ".jpg"){
$file_name = $userid."u.jpg";
copy ("$file", "/www/htdocs/user_images/$file_name") or die("Could
not copy file");

include "connect.php";
$result = mysql_query("INSERT INTO images (userid,name) VALUES
('$userid','$file_name')") or die ("Error in insert sql:".
mysql_error());
include "disconnect.php";

include("mem_header.php");
echo "Your file has been uploaded!<br><br>";
echo "<br>Your photo is pending approval<br><br>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=users.php\">";
include("mem_footer.php");
}else{
include("mem_header.php");
echo "Incorrect File Type. Your file must be a JPG<br><br>";
echo "Your file has NOT been uploaded!<br><br>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
URL=users.php?cmg=Upload\">";
include("mem_footer.php");
}

}
else {
include("mem_header.php");
echo "No file specified<br><br>";
echo "Your file has NOT been uploaded!<br><br>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
URL=users.php?cmg=Upload\">";
include("mem_footer.php");
}
?>

May 5 '06 #2

P: n/a
Chris - SyracuseCS.com wrote:
Here ya go. This is the script that I made for one of my sites. The
form takes the userid, and if the image gets uploaded ok it gets
renamed to useridu.jpg (100u.jpg) I only allow jpg's for this script.
It then gets inserted in an images table as unapproved (0). After I
approve the images the image gets renamed to userid.jpg (100.jpg) and
the users profile gets updated with the new picture.
Upload form

<?php

echo "Upload an image<br><br>";
echo "
<form action=\"uploader.php\" method=\"post\"
enctype=\"multipart/form-data\">

<input type=\"file\" name=\"file\" size=\"50\">
<br/>
<input type=\"submit\" value=\"Upload File\">
<input type='hidden' name='userid' value='$userid'>
</form>[ <a href=users.php class=\"menulink\">Back</a> ]<br><br>";

?>

uploader.php

<?php
global $userid;
if($file_name !="")
{
$image_type = strstr($file_name, '.');

if($image_type == ".jpg"){
$file_name = $userid."u.jpg";
copy ("$file", "/www/htdocs/user_images/$file_name") or die("Could
not copy file");

include "connect.php";
$result = mysql_query("INSERT INTO images (userid,name) VALUES
('$userid','$file_name')") or die ("Error in insert sql:".
mysql_error());
include "disconnect.php";

include("mem_header.php");
echo "Your file has been uploaded!<br><br>";
echo "<br>Your photo is pending approval<br><br>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2; URL=users.php\">";
include("mem_footer.php");
}else{
include("mem_header.php");
echo "Incorrect File Type. Your file must be a JPG<br><br>";
echo "Your file has NOT been uploaded!<br><br>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
URL=users.php?cmg=Upload\">";
include("mem_footer.php");
}

}
else {
include("mem_header.php");
echo "No file specified<br><br>";
echo "Your file has NOT been uploaded!<br><br>";
echo "<META HTTP-EQUIV=Refresh CONTENT=\"2;
URL=users.php?cmg=Upload\">";
include("mem_footer.php");
}
?>

Hi Chris,

Then I am assuming that you are recommending that I save individual
files.

I will try your script this afternoon and let you know how it works out.

Thank you so much for the recommendation.

Ciao . . . C.Joseph

And on the seventh day God said,
"I will rest . . . Murphy take over."
May 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.