Forum Moderators: coopster
****************************************
upload1.php
<html>
<head>
<title>Upload a picture to your site</title>
</head>
<body>
<?php
include ('includes/header.inc');
?>
<form name="form1" method="post" action="check_image1.php"
enctype="multipart/form-data">
<table border="0" cellpadding="5">
<tr>
<td>Location to upload picture<br>
<em>Example: Weddings</em></td>
<td><select name="folder_location">
<option value="" selected>Select a location....</option>
<option value="weddings">Weddings</option>
<option value="classic">Classic Decor</option>
<option value="dancefloors">Dance Floors</option>
<option value="stagedecor">Stage Decor</option>
</select>
</td>
</tr>
<td>Upload Image:</td>
<td><input name="image_filename" type="file" id="image_filename"></td>
</tr>
</table>
<br>
<em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
<p align="center"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html>
<?php
include ('includes/footer.inc');
?>
*******************************************
check_image1.php
<?php
//connect to the database
$link = mysql_connect("localhost", "admin", "pass")
or die("Could not connect: " . mysql_error());
mysql_select_db("gallery", $link)
or die (mysql_error());
//make variables available
$folder_location = $_POST['folder_location'];
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");
switch ($folder_location) {
case "weddings":
$ImageDir = "/home/pigsinbe/public_html/ballooncrew/gallery/weddings/";
break;
case "classic":
$ImageDir = "/home/pigsinbe/public_html/ballooncrew/gallery/classic/";
break;
case "dancefloors":
$ImageDir = "/home/pigsinbe/public_html/ballooncrew/gallery/dancefloors/";
break;
default:
echo "Sorry, but the location you selected was not correct.<br>";
echo "Please hit your browser's 'back' button and try again.";
}
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
$ImageName)) {
//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);
switch ($type) {
case 1:
$ext = ".gif";
break;
case 2:
$ext = ".jpg";
break;
case 3:
$ext = ".png";
break;
default:
echo "Sorry, but the file you uploaded was not a GIF, JPG, or " .
"PNG file.<br>";
echo "Please hit your browser's 'back' button and try again.";
}
//insert info into image table
$insert = "INSERT INTO gallery_weddings
(image_caption, image_date)
VALUES
('$image_caption', '$today')";
$insertresults = mysql_query($insert)
or die(mysql_error());
$lastpicid = mysql_insert_id();
$newfilename = $ImageDir . $lastpicid . $ext;
rename($ImageName, $newfilename);
}
?>
<html>
<head>
<title>Here is your pic!</title>
</head>
<body>
<h1>Success!</h1><br><br>
<p>Here is the picture you just uploaded to your server:</p>
<img src="gallery/weddings/<?php echo $lastpicid . $ext;?>" align="left">
<strong><?php echo $image_name;?></strong><br>
This image is a <?php echo $ext;?> image.<br>
It is <?php echo $width;?> pixels wide
and <?php echo $height;?> pixels high.<br>
It was uploaded on <?php echo $today;?>.
</body>
</html>
$folder_location = $_POST['folder_location'];
echo $folder_location;
In the success section, you have hard coded the wedding directory. You can make this change to use the selected folder.
<img src="gallery/<?php echo $folder_location;?>/<?php echo $lastpicid . $ext;?>" align="left">
Now for my next problem:
as you can see "check_image1.php" always writes to the same table in my mysql database "gallery_weddings", I would like to set it up so that depending on what folder the user selects to upload the picture at it uses that table. For examaple... If the user selects "Dance Floors" as the folder they would like to upload the picture to then instead of writing all the info to the table "gallery_weddings" I would like it to write that info to "gallery_dancefloors". Would I set up a switch as I did before or is there a better way to handle this? Thanks in advance!
$table = ''; // Initialize
switch ($folder_location) {
case "weddings":
$ImageDir = "/home/pigsinbe/public_html/ballooncrew/gallery/weddings/";
$table = 'gallery_weddings';
break;
case "classic":
$ImageDir = "/home/pigsinbe/public_html/ballooncrew/gallery/classic/";
$table = 'gallery_classic';
break;
case "dancefloors":
$ImageDir = "/home/pigsinbe/public_html/ballooncrew/gallery/dancefloors/";
$table = 'gallery_dancefloors';
break;
default:
echo "Sorry, but the location you selected was not correct.<br>";
echo "Please hit your browser's 'back' button and try again.";
}
Then modify your query statement:
$insert = "INSERT INTO $table
(image_caption, image_date)
VALUES
('$image_caption', '$today')";
$insertresults = mysql_query($insert)
or die(mysql_error());
Of course, this is assuming that your tables are all structured the same. Which brings up another interesting point ... if the table structures are the same, why not just add a column to the table to differentiate the gallery area?