|
Posted by Henri on 10/13/07 09:50
On Sat, 13 Oct 2007 08:30:54 +0000, Kye wrote:
> Can anybody suggest to me a good tutorial on how to upload images in php
> and have their location put into a MySQL database?
>
> I know that this is really basic but so are my PHP skills at the moment.
>
> TIA
> Kye.
some code to help figure the process out
include 'config.php'; // hold database name, location, user, pass etc
// we only upload jpg
$mimetypes = array("image/jpg", "image/jpeg", "image/jpe", "image/pjpeg");
$code = "";
// process post data
if (isset($_POST['filesubmitted'])) {
$tmp_name = $_FILES['file']['tmp_name'];
if (!is_uploaded_file($tmp_name))
$code = "filenotfounderror";
$file_size = $_FILES['file']['size'];
if ($file_size > $upload_file_size)
$code = "filesizeerror";
$mime_type = $_FILES['file']['type'];
if (!in_array($mime_type, $mimetypes))
$code = "filetypeerror";
} else
$code = "resubmit";
if ($code != "") {
// deal with error
session_start();
$_SESSION = array();
$_SESSION['formdata'] = $_POST;
header("Location: add.php?code=$code");
} else {
$document_root = $_SERVER['DOCUMENT_ROOT'];
$destination = $document_root . "/images/";
$file_name = $_FILES['file']['name'];
move_uploaded_file($tmp_name, $destination . $file_name);
if (is_file($destination . $file_name)) {
$oldumask = umask(0);
chmod($destination . $file_name, 0644);
umask($oldumask);
} else
die("Er... what the fuck happened?");
// insert new record in database
$file_name = $_FILES['file']['name'];
mysql_connect($db['host'], $db['user'], $db['password'])
or die (mysql_error());
mysql_select_db($db['name']) or die(mysql_error());
$query = "INSERT INTO $table (file_name) VALUES ('$file_name')";
$result = mysql_query($query);
if (!$result) {
$msg = 'Invalid query:' . mysql_error() . '<br>' . $query . '<br>';
die($msg);
}
mysql_close();
header("Location: done.php?");
}
Navigation:
[Reply to this message]
|