Web Hosting Talk







View Full Version : php script to upload images


michael-lane
07-03-2005, 10:22 AM
How do i make a php script to upload images in the best way, i know how from tutorials but i want to know which way is best and why cause i want efficiancy and stuff, there is a simple way i know of that i like:
save into database image code
<?php
/*get file from
pc*/
include("db.php");
$FILE = $_POST[file_url];
if($FILE == FALSE || $NAME== FALSE) {
echo "NO FILE WAS SELECTED!";
}
else {
mysql query("INSERT INTO images(img_code, img_name) VALUES('$FILE', '$NAME')", $db);
}
?>
generate image from database
<?php
/*read image from
database*/
include("db.php");
$image=$_GET[image];
if($image==TRUE) {
header("Content-type: image/gif");
$image_query = mysql_query("SELECT img_code FROM images WHERE img_name='$image'", $db);
$image_code = mysql_fetch_array($image_query);
echo "$image_code";
}
?>

but is that a good way to use

VolkNet
07-03-2005, 05:19 PM
If it works - it looks pretty good.

I would just leave it. :)

sasha
07-03-2005, 05:51 PM
if ( is_uploaded_file($_FILES['Filen_Name']['tmp_name']) && $image_details = @getImageSize($_FILES['File_Name']['tmp_name']) && $image_details[2] == '2' ) { // just for an example we tested if image is JPEG
$img = file_get_contents($_FILES['File_Name']['tmp_name']) ;
$query = "INSERT INTO images (original_name, size , width, height, img) VALUES (
'".addslashes($_FILES['File_Name']['name'])."' ,
'".strlen($img)."' , '".$image_details[0]."' , '".$image_details[1].'" , '".addslashes($img)."') " ;
.....



This is not very efficient as we read image 2 times, once for getImgeSize, and second time to get its contents, but more often then not real bottleneck in perfomance is not saving but reading images.

This way we can do one read, and have all image details we might need ready to use.