Forum Moderators: coopster
<?php
require_once("db.php");define("DN_IMG_PATH", "IMG/");
define("DN_PDF_PATH", "PDF/");
function handle_upload($file_id, $path, $suffix)
{
$source = $file_id['tmp_name'];
$dest = tempnam($path, "lamont-");
if ((isset($file_id['name'])) && ($file_id['name'] != ""))
{
copy($source, $dest);
if ($suffix!="")
{
$new = $dest.$suffix;
rename($dest, $new);
$dest = $new;
}
chmod($dest, 0777);
}
else
{
$dest = "";
}
return $dest;
}
$title = checkPostValue("ptitle", $_POST);
$paddr1 = checkPostValue("paddr1", $_POST);
$paddr2 = checkPostValue("paddr2", $_POST);
$ptown = checkPostValue("ptown", $_POST);
$pcounty = checkPostValue("pcounty", $_POST);
$postcode = checkPostValue("postcode", $_POST);
$ptype1 = checkPostValueNum("ptype1", $_POST);
$ptype2 = checkPostValueNum("ptype2", $_POST);
$ptype3 = checkPostValueNum("ptype3", $_POST);
$ptype4 = checkPostValueNum("ptype4", $_POST);
$pdescription = checkPostValue("pdescription", $_POST);
$psize = checkPostValueNum("psize", $_POST);
$psize2 = checkPostValueNum("psize2", $_POST);
$pcategory1 = checkPostValueNum("pcategory1", $_POST);
$pcategory2 = checkPostValueNum("pcategory2", $_POST);
$pcategory3 = checkPostValueNum("pcategory3", $_POST);
$pcategory4 = checkPostValueNum("pcategory4", $_POST);
$pcategory5 = checkPostValueNum("pcategory5", $_POST);
$pcategory6 = checkPostValueNum("pcategory6", $_POST);
$published = checkPostValueNum("published", $_POST);
$price = checkPostValue("price", $_POST);
$prpID = checkPostValue("prpID", $_POST);
if ($psize==false)
{
$psize=0;
}
if ($psize2==false)
{
$psize2=0;
}
if ($psize<0)
{
$psize=0;
}
if ($psize2<0)
{
$psize2=0;
}
if (($psize==0) && ($psize2!=0))
{
$psize = $psize2;
}
if (($psize!=0) && ($psize2==0))
{
$psize2 = $psize;
}
if ($psize>$psize2)
{
$x = $psize;
$psize = $psize2;
$psize2=$x;
}
if ($_FILES['pimage_file']['size']!=0)
{
if ($prpID)
{
delete_old_image($dbConn, $prpID);
}
$pimage_temp = handle_upload($_FILES['pimage_file'],DN_IMG_PATH, ".jpg");
$pimage_fname = $_FILES['pimage_file']['name'];
}
else
{
if ($prpID)
{
$result = getPropertyByID($dbConn, $prpID);
$row = mysql_fetch_assoc($result);
$pimage_temp = $row['prp_image_temp_name'];
$pimage_fname = $row['prp_image_orig_name'];
}
}
if ($_FILES['ppdf_file']['size']!=0)
{
if ($prpID)
{
delete_old_pdf($dbConn, $prpID);
}
$ppdf_temp = handle_upload($_FILES['ppdf_file'],DN_PDF_PATH, ".pdf");
$ppdf_fname = $_FILES['ppdf_file']['name'];
}
else
{
if ($prpID)
{
$result = getPropertyByID($dbConn, $prpID);
$row = mysql_fetch_assoc($result);
$ppdf_temp = $row['prp_pdf_temp_name'];
$ppdf_fname = $row['prp_pdf_orig_name'];
}
}
print $result_array = array
("title"=>$title,
"addr1"=>$paddr1,
"addr2"=>$paddr2,
"addr3"=>"",
"town"=>$ptown,
"county"=>$pcounty,
"postcode"=>$postcode,
"ptype1"=>$ptype1,
"ptype2"=>$ptype2,
"ptype3"=>$ptype3,
"ptype4"=>$ptype4,
"pdescription"=>$pdescription,
"psize"=>$psize,
"psize2"=>$psize2,
"pcategory1"=>$pcategory1,
"pcategory2"=>$pcategory2,
"pcategory3"=>$pcategory3,
"pcategory4"=>$pcategory4,
"pcategory5"=>$pcategory5,
"pcategory6"=>$pcategory6,
"published"=>$published,
"pimage_name"=>$pimage_fname,
"pdf_name"=>$ppdf_fname,
"pimage_temp"=>$pimage_temp,
"pdf_temp"=>$ppdf_temp,
"price"=>$price);
if (isset($_POST['prpID']) && $_POST['prpID']!="")
{
update_property($dbConn, $result_array, $_POST['prpID']);
}
else
{
insert_property($dbConn, $result_array);
}
printf("<script>location.href='index.php'</script>");
?>
With regards rick
If that's not always the case, explain a little more how you want the folder and I can look at it closer.
I really hope this makes sense as i said ive been at this all day and its getting me down now lol
*EDIT*
PDF's will always be uploaded to Listings/admin/PDF and images will always be uploaded to Listings/admin/IMG
Thanks Rick.
$fileName = getFileName("/some folder/ some other folder/file.pdf");
function getFileName($path){
return(end(explode("/", $path)));
}//getFileName
if anything id rather it insert it with no file path at all, if it just inserted lamont-NEWNAME.jpg
You can call basename() to get just the filename, minus all the directory names. I can't quite make out from your code which variables hold the data destined for the DB, but let's say $img_fname contains the file's full path, you'd do something like:
$filename_to_put_in_db = basename($img_fname);
Hope this helps.