PDA

View Full Version : need help from image upload experts to database


nanny
07-02-2009, 09:06 AM
Hi for the life of me I can't work out why these uploads are not updating in the database. They will do on my local machine but when I go to the remote server - dead - nothing - everything stays as is.
OK the code:

define ('MAX_FILE_SIZE', 1048576);
if (array_key_exists('upload', $_POST)) {

$original = $_POST['image'];
$title = $_POST['title'];
if (empty($title)) {
$error['title'] = 'Please give a title as a heading';
}
$caption = $_POST['caption'];
if (empty($caption)) {
$error['caption'] = 'Please select a caption for search engines';
}
$descr = $_POST['descr'];
if (empty($descr)) {
$error['descr'] = 'Please a description';
}
else if (strlen ($_POST['descr']) > 181) {
$error['classDescription'] = 'Your description is too long, please do not submit more then 180 characters';
}
// define constant for upload folder
define('UPLOAD_DIR', '../images/gallery/');

// replace any spaces in original filename with underscores
// at the same time, assign to a simpler variable
$iffy = array(' ', '-', '(', ')');
$file = str_replace($iffy, '_', $_FILES['imagename']['name']);
// convert the maximum size to KB
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;

// check that file is within the permitted size
if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
// check that file is of an permitted MIME type
foreach ($permitted as $type) {
if ($type == $_FILES['image']['type']) {
$typeOK = true;
break;
}
}

if ($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0:
include('create_both.inc.php');
break;
case 3:
$result = "Error uploading $filename. Please try again.";
default:
$result = "System error uploading $filename. Contact webmaster.";
}
}
elseif ($_FILES['image']['error'] == 4) {
$result = 'No file selected';
}
else {
$result = "$filename cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
}
}


create_both.inc:


// define constants
define('THUMBS_DIR', '../images/gallery/thumbnail/');
define('MAX_WIDTH', 450);
define('MAX_HEIGHT', 227);
define('THUMBS_DIR2', '../images/gallery/thumb/');
define('MAX_WIDTH2', 120);
define('MAX_HEIGHT2', 120);

$id = $_GET['id'];
// process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
$original = $_FILES['image']['tmp_name'];
// begin by getting the details of the original
list($width, $height, $type) = getimagesize($original);
// calculate the scaling ratio
if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
$ratio = 1;
}
elseif ($width > $height) {
$ratio = MAX_WIDTH/$width;
}
else {
$ratio = MAX_HEIGHT/$height;
}
if ($width <= MAX_WIDTH2 && $height <= MAX_HEIGHT2) {
$ratio2 = 1;
}
elseif ($width > $height) {
$ratio2 = MAX_WIDTH2/$width;
}
else {
$ratio2 = MAX_HEIGHT2/$height;
}
// strip the extension off the image filename
$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));

// move the temporary file to the upload folder
$moved = move_uploaded_file($original, UPLOAD_DIR.$_FILES['image']['name']);
if ($moved) {
$result = $_FILES['image']['name'].' successfully uploaded; ';
$original = UPLOAD_DIR.$_FILES['image']['name'];
}
else {
$result = 'Problem uploading '.$_FILES['image']['name'].'; ';
}
// create an image resource for the original
switch($type) {
case 1:
$source = @ imagecreatefromgif($original);
if (!$source) {
$result = 'Cannot process GIF files. Please use JPEG or PNG.';
}
break;
case 2:
$source = imagecreatefromjpeg($original);
break;
case 3:
$source = imagecreatefrompng($original);
break;
default:
$source = NULL;
$result = 'Cannot identify file type.';
}
// make sure the image resource is OK
if (!$source) {
$result = 'Problem copying original';
}
else {
// calculate the dimensions of the thumbnail
$thumb_width = round($width * $ratio);
$thumb_height = round($height * $ratio);
$thumb_width2 = round($width * $ratio2);
$thumb_height2 = round($height * $ratio2);
// create an image resource for the thumbnail
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
// create the resized copy
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
// save the resized copy
// create an image resource for the thumbnail
$thumb2 = imagecreatetruecolor($thumb_width2, $thumb_height2);
// create the resized copy
imagecopyresampled($thumb2, $source, 0, 0, 0, 0, $thumb_width2, $thumb_height2, $width, $height);
// save the resized copy
switch($type) {
case 1:
if (function_exists('imagegif')) {
$success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb2.jpg', 50);
$thumb_name2 = $name.'_thb2.jpg';
}
if (function_exists('imagegif')) {
$success = imagegif($thumb2, THUMBS_DIR.$name.'_thb.gif');
$thumb_name = $name.'_thb.gif';
}
else {
$success = imagejpeg($thumb2, THUMBS_DIR2.$name.'_thb2.jpg', 50);
$thumb_name2 = $name.'_thb2.jpg';
}
break;
case 2:
$success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
$thumb_name = $name.'_thb.jpg';
$success = imagejpeg($thumb2, THUMBS_DIR2.$name.'_thb2.jpg', 100);
$thumb_name2 = $name.'_thb2.jpg';
break;
case 3:
$success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
$thumb_name = $name.'_thb.png';
$success = imagepng($thumb2, THUMBS_DIR2.$name.'_thb2.png');
$thumb_name2 = $name.'_thb2.png';
}
if ($success) {
$result .= "$thumb_name created";
$result .= "$thumb_name2 created";
}
if ($success) {
$update = "UPDATE photo SET image= '$original', thumbnail = '$thumb_name' , thumbnail2 = '$thumb_name2' , title = '$title', caption = '$caption', descr = '$descr' WHERE id = $id";
mysql_query($update);
header ("Location: gallery.php");

}
else {
$result .= 'Problem creating thumbnail';
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
imagedestroy($thumb2);
}
}



Yes I have 777 chmod the image folders and yes the images are going there as expected in their resized format. But it won't update any of the image location/name or any other part of the form e.g. caption

Really frustrating - what in the world am I doing wrong for the server.
Thanks....

jaymo
07-11-2009, 05:32 PM
If your moving from local server to your web host. Make sure you have changed your connection script accordingly, so it can connect to the database. I've seen that mistake in the past