Hello!
First post and very new to php - attempting to build some kind of CMS (read a couple of books and want to get something up and running for a community web site).
I have basically been working for the past week or so on the following script. First part is obv. just the form but the second part seems to have a problem - it uploads a file in the right dir and inserts all the info into the d/b table but does not add the file extensions for me?
Sorry to be an annoying newbie but I've done a lot of searching with no luck 🙁 Please help me out if you can - would be much appreciated!!
form script:
<form enctype="multipart/form-data" method="post" action="upload.php">
<b>Upload image</b><br>
Select image: <input name="file" type="file"><br>
Title: <input name="title" type="text"><br>
Description: <textarea name="descr"></textarea><br>
Alt-text: <input name="alt" type="text"><br>
<input type="submit" value="Upload">
</form>
transaction script:
<?php
// define the base image dir
$base_img_dir = "../../img/";
// define location of image conversion programs
$img_conv_dir = "/bin/";
// define database table containing image info
$img_table = "images";
// connect with database
mysql_connect("localhost", "root", "password");
mysql_select_db("campingwarehouse");
// generate unique id for use in filename
$uniq = uniqid("");
// new file name
$filename = $base_img_dir.$uniq;
// move uploaded file to destination
move_uploaded_file($HTTP_POST_FILES["file"]["tmp_name"], $filename);
// retrieve image info
$imginfo = getimagesize($filename);
// handle image according to type
switch ($imginfo[2]) {
case 1: // gif
// convert gif to png using shell command
$command = $img_conv_dir."gif2png $filename";
exec($command);
// remove original gif file and rename converted png
unlink($filename);
rename("$filename.png", $filename);
// check png image by loading and saving the file
// to prevent wrong uploaded files and errors
$img = imagecreatefrompng($filename);
imagepng($img, $filename);
imagedestroy($img);
// set image type to png
$img_type = "PNG";
break;
case 2: // jpeg
// check jpeg image by loading and saving the file
// to prevent wrong uploaded files and errors
$img = imagecreatefromjpeg($filename);
imagejpeg($img, $filename);
imagedestroy($img);
// set image type to jpeg
$img_type = "JPG";
break;
case 3: // png
// check png image by loading and saving the file
// to prevent wrong uploaded files and errors
$img = imagecreatefrompng($filename);
imagepng($img, $filename);
imagedestroy($img);
// set image type to png
$img_type = "PNG";
break;
case 4: // bmp
// rename file to bmp
rename($filename, "$filename.bmp");
// convert bmp to png using shell command
$command = $img_conv_dir."bmptoppm $filename.bmp | ".
$img_conv_dir."pnmtopng > $filename";
exec($command);
// remove original bmp
unlink("$filename.bmp");
// check png image by loading and saving the file
// to prevent wrong uploaded files and errors
$img = imagecreatefrompng($filename);
imagepng($img, $filename);
imagedestroy($img);
// set image type to png
$img_type = "PNG";
break;
default:
break;
}
// retrieve image file size
$imgbytes = filesize($filename);
// insert image into db
mysql_query("INSERT INTO $img_table (img_file, img_type, img_height,
img_width, img_bytes, img_title, img_descr, img_alt)
VALUES('$uniq', '$img_type', ".$imginfo[1].", ".$imginfo[0].",
$imgbytes, '".addslashes($HTTP_POST_VARS["title"])."', '".
addslashes($HTTP_POST_VARS["descr"])."',
'".addslashes($HTTP_POST_VARS["alt"])."');");
// display some information
echo "Image uploaded.<br><img src=\"img.php?f($uniq)+x(300)\"><br>".
"URL: img.php?f($uniq)";
?>
To try and be specific about the prob - after using the upload form it shows a page with a broken image link (which i think is due to there being no extension on the image filename).
If i go into my images folder manually and add the correct file extension, the image appear perfectly. Very strange - very annoying!! I hate being the new kid!!
Thanks again - Gareth