Hey,
Here is a simple upload page i wrote not to long ago that allows a user to upload an image and then i can take that info on that image and put it into a database to keep track of it and later pull that info out to display it on a web page.
The script also creates a folder for the users images and puts the image in that folder and not into the database as this may cause issuse later on i'm told.
It also emails me when a user adds/updates an image, this way i can check the image before i allow it to be viewed by the public.
I hope this helps.
It has been working great for me but if anyone see a better way to do it let me know.
<?
session_start();
$msg1 = "";
$userid = $_SESSION[userid];
$pro_id = $_SESSION[pro_id];
$pro_name = $_SESSION[pro_name];
$username = $_SESSION[username];
if ($op != "ds") {
$show_form = "yes";
}
//-------------//
if ($_POST['submit'] == 'Upload'){
if (($username == "") || ($pro_id == "") || ($pro_name == "")){
$msg1 = "Error.";
$error = "You must be logged in.";
} else {
$msg1 = "";
$error = "";
$tempfile = $_FILES[img1][tmp_name];
$image_type = $_FILES[img1][type];
$image_size = $_FILES[img1][size];
if ($_FILES[img1][tmp_name] == "") {
$msg1 = "There were errors:";
$error = "No file selected.<br>";
}
if ($_FILES[img1][size] == 0) {
$msg1 = "There were errors:";
$error .= "File size cannot equal 0.<br>";
}
//--------------------------//
if ($error == "") {
if ($image_type == "image/jpeg") {
$image_ext = ".jpg";
} else {
if ($image_type == "image/gif")
$image_ext = ".gif";
}
if (($image_type != "image/jpeg") && ($image_type != "image/gif")) {
$msg1 = "There were errors:";
$error .= "File must be a gif or a jpeg.<br>";
}
if ($_FILES[img1][size] > 44000) {
$msg1 = "There were errors:";
$error .= "File size cannot be greater 44k.<br>";
}
}
if ($error == "") {
$show_form = "";
$show_upload = "yes";
$image_dir = "upload_images/";
$image_num_id = $_SESSION[image_name];
$user_dir = $image_dir."/".$username;
$dir = @opendir($user_dir);
$image_name = ($image_num_id.$image_ext);
$image_path = ($image_dir.$username."/".$image_name);
$dir = @opendir($user_dir);
if(!@opendir($user_dir)) {
// creates missing dir
$oldmask = umask(0);
mkdir($image_dir.$username,0777);
umask($oldmask);
opendir($user_dir);
$msg1 .= "Folder did not exsist so it was created and is now open<br><Br>";
} else {
opendir($user_dir);
$msg1 .= "Folder exsists and is now open<br><Br>";
}
if(!copy($tempfile,$user_dir."/".$image_name)){
{
$msg1 .= "Could not upload file<br><Br>";
}
} else {
$msg1 .= "uploaded file to $user_dir<br><Br>";
closedir($dir);
}
$db_name = "****";
$table_name = "images";
$connection = @mysql_connect("****","****","****") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql2 = "select image_name from $table_name where image_name ='$image_name'";
$result = mysql_query($sql2,$connection) or die("Couldn't execute query 1.");
if (mysql_num_rows($result) == 0) {
$sql = "INSERT INTO $table_name (userid, pro_id, pro_name, image_name, image_size, image_type, image_path) VALUES ('$userid','$pro_id', '$pro_name', '$image_name', '$image_size', '$image_type', '$image_path')";
$result = mysql_query($sql, $connection) or die(mysql_error());
$mail_to = "me@mysite";
$mail_subject = "$username has just added a product";
$mail_body = "$username has just added a product which need to be approved\n";
$mail_body .= "Their product is $pro_name\n";
$mail_body .= "The product id is $pro_id\n";
if(mail($mail_to,$mail_subject,$mail_body)) {
$mailmsg = "Info has been Sent";
} else {
$mailmsg = "Could not send mail";
exit;
}
} else {
$sql = "UPDATE $table_name SET pro_name ='$pro_name', image_name ='$image_name', image_size ='$image_size', image_type ='$image_type', image_path ='$image_path' where pro_id ='$pro_id'";
$result = mysql_query($sql,$connection) or die("Couldn't execute query 2.");
$mail_to = "me@mysite";
$mail_subject = "$username has just updated a product";
$mail_body = "$username has just updated a product\n";
$mail_body .= "Their product is $pro_name\n";
$mail_body .= "The product id is $pro_id\n";
if(mail($mail_to,$mail_subject,$mail_body)) {
$mailmsg = "Info has been Sent";
} else {
$mailmsg = "Could not send mail";
exit;
}
}
}
}
}
//-------------//
$form_block = "
<table>
<tr>
<td>
Upload Image:
<p>
<form action=\"uload.php\" method=\"post\" enctype=\"multipart/form-data\">
<input type=\"hidden\" name=\"op\" value=\"ds\">
<input type=\"file\" name=\"img1\" size=\"30\">
<p>
<input type=\"submit\" name=\"submit\" value=\"Upload\"> <input type=\"reset\" value=\"Reset\"></p>
</form>
<p>
$msg1
<br><br>
$error
</td>
</tr>
</table>
";
//---------------//
$upload_block = "
<table>
<tr>
<td class=\"wb\">
Upload Successful!!
<p>
The image for <b>$pro_name</b> has been uploaded.
</td>
</tr>
<tr>
<td>
<p><a href=\"area.php\">Done</a></p>
</td>
</tr>
</table>
";
//---------------//
if ($show_form == "yes") {
$display_block = $form_block;
} else
if ($show_upload == "yes") {
$display_block = $upload_block;
}
?>
<html>
<head>
<title>Upload Images</title>
</head>
<? include ("top.txt");?>
<? print "$display_block"; ?>
<? include ("bot.txt");?>
</body>
</html>
Again I hope this help.
troinfo