in addition to above...
Since this is getting complicated, I thought it would be best to include the full page of code. Sorry for the length, but I don't know how else to explain it.
<?php
//success.php
//Populates the advertisement table with the submitted data. Returns a success/failure message to the user.
//Give the user the option to post another ad or return to the main page.
include "common.php";
html_header();
function form_validate() {
//Validate input data, check for required inputs and format for storage.
global $id;
//Check for required inputs.
if(empty($_REQUEST['adSection'])) error_message("Select an ad category.");
if(empty($_REQUEST['adPrice'])) error_message("Please enter a price for your item.");
if(empty($_REQUEST['adContact'])) error_message("Please enter contact information.");
//Validate the input for the Price field
//Begin by exploding the string by the decimal character;
$str = explode(".", $_REQUEST['adPrice']);
//Count the array to determine how to proceed. If count = 1, then no decimal places exist.
//If count is > 1, then at least one decimal was input into the string.
$decimal = count($str);
//Test our string or strings for non_numeric characters
if($decimal == 1) //one array item = no decimal characters used.
if(ereg("[^0-9]", $str[0])) {
error_message("\$" . $_REQUEST['adPrice'] . " has an invalid character.");
@header("Location: [url]http://kanga.ddts.net:8008/Working/adload.php[/url]");
exit;
}
else $id = update_table();
else if($decimal == 2) { //two array items = exactly one decimal character used.
foreach($str as $chunk) {
$test = ereg("[^0-9]", $chunk);
if($test) $flag = 1;
}
if($flag) {
error_message("\$" . $_REQUEST['adPrice'] . " has an invalid character.");
@header("Location: [url]http://kanga.ddts.net:8008/Working/adload.php[/url]");
exit;
}
else $id = update_table();
}
else { //more than two array items = more than two decimals used. Invalid entry.
error_message("\$" . $_REQUEST['adPrice'] . " has an invalid character.");
@header("Location: [url]http://kanga.ddts.net:8008/Working/adload.php[/url]");
exit;
}
//Test for a file path in adFullimg
if(!empty($_REQUEST['adFullimg'])) {
$adFullimg_path = upload_file($id);
$thmb_path = thumbnail($id);
$_SESSION['r2'] = update_ad($id, $adFullimg_path, $thmb_path);
}
$_SESSION['id'] = $id;
}
function update_table() {
//Writes a basic INSERT into the update table to generate an adPostID number to be appended
//to uploaded files.
global $SES_member_ID, $adSection, $adDescription, $adPrice, $adPriceComment,
$adContact, $adContactEmail, $adThumbimg, $adFullimg;
$SES_member_ID = $_SESSION['SES_member_ID'];
$adSection = $_REQUEST['adSection'];
$adPrice = $_REQUEST['adPrice'];
$adContact = $_REQUEST['adContact'];
if(!empty($_REQUEST['adDescription'])) $adDescription = $_REQUEST['adDescription'];
else $adDescription = "No Description Given.";
if(!empty($_REQUEST['adPriceComment'])) $adPriceComment = $_REQUEST['adPriceComment'];
else $adPriceComment = "Shipping or Billing Information Not Supplied. Contact seller directly.";
if(!empty($_REQUEST['adContactEmail'])) $adContactEmail = $_REQUEST['adContactEmail'];
else $adContactEmail = "No E-mail Address Given.";
if(!empty($_FILES['adFullimg']['name'])) {
$adFullimg = NULL;
$adThumbimg = NULL;
}
else {
$adFullimg = NULL;
$adThumbimg = "No Photo Given.";
}
$link_id = db_connect();
$query = "INSERT INTO advertisement VALUES(NULL, '$SES_member_ID', '$adSection', '$adDescription',
'$adPrice', '$adPriceComment', '$adContact', '$adContactEmail', '$adThumbimg', '$adFullimg',
'N', curdate())";
$result = mysql_query($query);
if(!$result) error_message(sql_error());
else{
$id = mysql_insert_id($link_id); //get the adPostID automatically generated by INSERT
mysql_close($link_id);
}
return $id;
}
function upload_file($id) {
//Uploads an image file to the server if the user sends one with the ad form.
global $upload_adFullimg, $upload_adFullimg_name, $upload_adFullimg_size, $upload_adFullimg_type,
$upload_adFullimg_dir, $WINDIR;
$upload_adFullimg = $_FILES['adFullimg']['tmp_name'];
$upload_adFullimg_name = $_FILES['adFullimg']['name'];
$upload_adFullimg_size = $_FILES['adFullimg']['size'];
$upload_adFullimg_type = $_FILES['adFullimg']['type'];
// $upload_adFullimg_dir = "/var/www/html/Working/images";
// $WINDIR = $_FILES['WINDIR'];
if(isset($WINDIR)) {
echo "This ran";
$upload_adFullimg = str_replace("\\\\","\\\", $upload_adFullimg);
}
$filename = basename($upload_adFullimg_name);
$stored_filename = $id . "_" . $filename;
if($upload_adFullimg_size <= 0) die ();
if(!move_uploaded_file($upload_adFullimg, "$upload_adFullimg_dir/$stored_filename"))
die ("Can't copy $upload_adFullimg_name to $filename.");
if(isset($WINDIR) && unlink($upload_adFullimg)) die ("Can't delete the file $upload_adFullimg_name.");
//echo "$filename has been successfully uploaded.<br>";
//echo "Filesize: " . number_format($upload_adFullimg_size) . "<br>";
//echo "Filetype: $upload_adFullimg_type<br>";
$adFullimg_path = "$upload_adFullimg_dir/$stored_filename";
return $adFullimg_path;
}
function thumbnail($id) {
//Generates a thumbnail image out of the previously uploaded image.
global $upload_adFullimg_dir, $thumb_w, $thumb_h, $src_img, $new_h, $new_w;
$new_h = 150;
$new_w = 150;
$full_img = explode("/", $_FILES['adFullimg']['type']);
if(eregi("pjpeg|jpeg|jpg", $full_img[1])){
$path = "$upload_adFullimg_dir/$id" . "_" . basename($_FILES['adFullimg']['name']);
$src_image = imagecreatefromjpeg($path);
}
else if(eregi("png|x-png", $full_img[1])) {
$path = "$upload_adFullimg_dir/$id" . "_" . basename($_FILES['adFullimg']['name']);
$src_image = imagecreatefrompng($path);
}
else {
error_message($_FILES['adFullimg']['type'] . " is not a valid file type for this site.");
@header("Location: [url]http://kanga.ddts.net:8008/Working/adload.php[/url]");
exit;
}
//Preserve aspect ratio.
//Code courtesy of Christian Heilmann @ [url]http://www.onlinetools.org/articles/creating_thumbnails_all.php[/url]
$old_x = imagesx($src_image);
$old_y = imagesy($src_image);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img = imagecreate($thumb_w, $thumb_h);
imagecopyresized($dst_img,$src_image,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if(eregi("pjpeg|jpeg|jpg", $full_img[1])) {
$thmb_path = "$upload_adFullimg_dir/$id"."thmb"."_".basename($_FILES['adFullimg']['name']);
imagejpeg($dst_img, $thmb_path);
}
else {
$thmb_path = "$upload_adFullimg_dir/$id"."thmb"."_".basename($_FILES['adFullimg']['name']);
imagepng($dst_img, $thmb_path);
}
imagedestroy($dst_img);
imagedestroy($src_image);
return $thmb_path;
}
function update_ad($id, $adFullimg_path, $thmb_path) {
//Writes an SQL update statement to store in the advertisement table the path to the full image
//and thumbnail image.
$link_id = db_connect();
$query = "UPDATE advertisement SET adFullimg = '$adFullimg_path', adThumbimg = '$thmb_path'
WHERE adPostID = '$id'";
$result = mysql_query($query);
if($result) $c = 1;
else $c = 0;
return $c;
}
//Verify that an authenticated user has reached this page. If not, return them to the home page.
if(empty($_SESSION['SES_member_ID'])){
@header("Location: [url]http://kanga.ddts.net:8008/Working/ebdefault.php[/url]");
exit;
}
else form_validate();
//Call the next page according to the criteria
/*
if($_SESSION['r2'] && !empty($_REQUEST['adFullimg'])) {
echo "What?";
echo "<meta http-equiv='Refresh' content='0; URL=http://kanga.ddts.net:8008/Working/preview.php'>";
}
else if(!$_SESSION['r2'] && !empty($_REQUEST['adFullimg'])) {
echo "What up?";
echo "<meta http-equiv='Refresh' content='0; URL=http://kanga.ddts.net:8008/Working/failure.php'>";
}
else {
echo "What up, doc?";
echo "<meta http-equiv='Refresh' content='0; URL=http://kanga.ddts.net:8008/Working/preview.php'>";
}
*/
if ($_REQUEST['adFullimg'])
{
echo "IF";
if ($_SESSION['r2'])
{
echo "What?";
echo "<meta http-equiv='Refresh' content='5; URL=http://kanga.ddts.net:8008/Working/preview.php'>";
}
else if (!$_SESSION['r2'])
{
echo "What up?";
echo "<meta http-equiv='Refresh' content='0; URL=http://kanga.ddts.net:8008/Working/failure.php'>";
}
}
else
{
echo "What up doc?";
echo "<meta http-equiv='Refresh' content='0; URL=http://kanga.ddts.net:8008/Working/preview.php'>";
}
html_footer();
?>