I have some, but I still don't know how to modify it. I want images names to be the same in the folder and in the mysql table and to ble like this : image_dir/imagename.id.jpg. Now in the table there is only image_caption which the user put, and in the folder there is the id like image_name (1, 2,3 ...)
this is the table :
Code: Select all
CREATE TABLE IF NOT EXISTS images (
image_id INT(11) NOT NULL AUTO_INCREMENT,
image_caption VARCHAR(255) NOT NULL,
username VARCHAR(255) NOT NULL,
image_date DATE NOT NULL,
PRIMARY KEY (image_id)
file:
Code: Select all
<? ob_start(); //Its turning on the output buffer. So any output is kept in the buffer. And ob_flush() is to flush the buffer ?><?php
session_start();
$username = $_SESSION['username'];
echo $username;
include "config.php";
//make variables available
if (isset($_POST['Submit'])) {
$image_caption = $_POST['image_caption'];
//$username = $_POST['user'];
$image_tempname = $_FILES['image_filename']['name'];
date_default_timezone_set('Europe/Helsinki');
$today = date("Y-m-d");
//upload image and check for image type
//make sure to change your path to match your images directory
$ImageDir ="img_user/";
$ImageThumb = $ImageDir . "thumbs/";// za syzdavane na miniaturi
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
$ImageName)) {
//get info about the image being uploaded
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.";
}
if ($type > 3) {
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.";
} else {
//insert info into image table
$insert = "INSERT INTO images
(image_caption, username, image_date)
VALUES
('$image_caption', '$username', '$today')";
$insertresults = mysql_query($insert)
or die(mysql_error());
$lastpicid = mysql_insert_id();
$newfilename = $ImageDir . $lastpicid . $ext;
if ($type == 2) {
rename($ImageName, $newfilename);
} else {
if ($type == 1) {
$image_old = imagecreatefromgif($ImageName);
} elseif ($type == 3) {
$image_old = imagecreatefrompng($ImageName);
}
//"convert" the image to jpg
$image_jpg = imagecreatetruecolor($width, $height);
imagecopyresampled($image_jpg, $image_old, 0, 0, 0, 0,
$width, $height, $width, $height);
imagejpeg($image_jpg, $newfilename);
imagedestroy($image_old);
imagedestroy($image_jpg);
}
$newthumbname = $ImageThumb . $lastpicid . ".jpg";
//get the dimensions for the thumbnail
$thumb_width = $width * 0.10;
$thumb_height = $height * 0.10;
//create the thumbnail
$largeimage = imagecreatefromjpeg($newfilename);
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $largeimage, 0, 0, 0, 0,
$thumb_width, $thumb_height, $width, $height);
imagejpeg($thumb, $newthumbname);
imagedestroy($largeimage);
imagedestroy($thumb);}
}
}else{
?>
<html>
<head>
<title>Upload your pic to our site!</title>
</head>
<body>
<form name="form1" method="post" action='<?=$_SERVER['PHP_SELF']?>'
enctype="multipart/form-data">
<table border="0" cellpadding="5">
<tr>
<td>Image Title or Caption<br>
<em>Example: You talkin' to me?</em></td>
<td><input name="image_caption" type="text" id="image_caption" size="55"
maxlength="255"></td>
</tr>
<tr>
</tr>
<td>Upload Image:</td>
<td><input name="image_filename" type="file" id="image_filename"></td>
</tr>
</table>
<br>
<em>Acceptable image formats include: GIF, JPG/JPEG, and PNG.</em>
<p align="center"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html><?php }?><? ob_flush(); ?>