move_uploaded_file frustration
Hey there,
I am trying to build a film scouts website where the website manager can upload images and text. I have modified your chapter 13 transact-article to achieve the text side of things and am now trying to incorporate your chapter 7 image uploading script. I have sucessfully got the script working as written in the book but i can't get it to connect to the datbase when it is inserted into the transact article page. I have narrowed it down to the if (move_uploaded_file.. part of the script. If I comment this and the closing parentheses the script connects and adds the information to the script (however doesn't obviously move the image to the folder). If I leave this in it simply won't connect to the database. I had to modify the script in order to achieve the connection as the database part of check_image.php doesn't work either. any thoughts? below is the script as it sits in my page:
<?php
session_start();
require_once 'conn.php';
require_once 'http.php';
if (isset($_REQUEST['action'])) {
switch ($_REQUEST['action']) {
case 'Upload Image':
$image_tempname = $_FILES['image_filename']['name'];
$ImageDir ="img/dbimg/";
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
$ImageName)) {
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.";
}
$sql = "INSERT INTO cms_images " .
"(image_caption, image_username, page_id, image_date) " .
"VALUES ('" . $_POST['image_caption'] .
"','" . $_POST['pageid'] .
"'," . $_SESSION['user_id'] . ",'" .
date("Y-m-d H:i:s", time()) . "')";
mysql_query($sql, $conn)
or die('Could not insert content; ' . mysql_error());
$lastpicid = mysql_insert_id();
$newfilename = $ImageDir . $lastpicid . $ext;
rename($ImageName, $newfilename);
}
if ($_POST['pageid'] == 'crew') {
redirect('crew.php?imageupload=yes');
break;
}
}
} else {
redirect('index.php');
}
?>
Thanks for help in advance.
|