Forum Moderators: coopster
<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");
if(isset($_REQUEST['submit']))
{
$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];
if($imgtype=="image/jpeg" ¦¦ $imgtype=="image/jpg" ¦¦ $imgtype=="image/pjpeg" ¦¦ $imgtype=="image/gif" ¦¦ $imgtype=="image/x-png" ¦¦ $imgtype=="image/bmp")
{
$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>
Now I want to retrieve the row data into an html page including the image.
Please let someone help.
Austin
$query = "SELECT image, image_type from img_tab1 where id=$id";
$result = @MYSQL_QUERY($query);$data = @MYSQL_RESULT($result,0,"image");
$type = @MYSQL_RESULT($result,0,"image_type");header( "Content-type: $type");
echo $data;
Two points to remember:
I have quickly tried the code you gave but what I get is an empty page.
The code is below:
<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");
$query = "SELECT image, image_type from img_tab1 where id=$id";
$result = @MYSQL_QUERY($query);
$data = @MYSQL_RESULT($result,0,"image");
$type = @MYSQL_RESULT($result,0,"image_type");
echo $data;
?>
Please you can go through and see where the error is.
Thanks.
Austin
I have tried including headers as instructed but I get the error below:
Warning: Cannot modify header information - headers already sent by (output started at D:\wamp\www\imagesfolder\image_display.php:2) in D:\wamp\www\imagesfolder\image_display.php on line 16
And this is the code being reffered to in line 16:
<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");
$query = "SELECT image, image_type from img_tab1 where id=$id";
$result = @MYSQL_QUERY($query);
$data = @MYSQL_RESULT($result,0,"image");
$type = @MYSQL_RESULT($result,0,"image_type");
header( "Content-type: $type");
echo $data;
?>
Please go through and let me know where I went wrong.
Thanks
Austin
I have just finished an upload script that adds the data also to a mysql DB, it is very flexible and easy to work with it does what I need and it looks like it will do what you need too.
The only thing missing on that page is the finished image saving script that now can resize the saved images I finished that yesterday, if you want it just ask and I will post it at the bottom of that thread.
[edited by: jatar_k at 4:49 pm (utc) on Feb. 14, 2008]
[edit reason] fixed link [/edit]
I have successfully uploaded image to mysql database using a form: This is the code:
<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");
if(isset($_REQUEST['submit']))
{
$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];
if($imgtype=="image/jpeg" ¦¦ $imgtype=="image/jpg" ¦¦ $imgtype=="image/pjpeg" ¦¦ $imgtype=="image/gif" ¦¦ $imgtype=="image/x-png" ¦¦ $imgtype=="image/bmp")
{
$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>
Now I need to display the row data including the image, can you please offer a code that will solve the problem.
Thanks
Austin
[edited by: jatar_k at 11:22 pm (utc) on Feb. 15, 2008]
$uploaddir = "../apartments/images/";
$target = "350";
foreach ($_FILES as $key=>$value){
if ($value[size] > '0'){$add = "../apartments/images/".$propname."+".$key.".jpg";
$sql = "insert into images values('', '$propname-$key.jpg', '$propname', '$key')";
if (mysql_query($sql, $dblink)){
echo "IMAGE added!<br><br>";
}else{
echo "Something was wrong<br><br>";
}
copy($_FILES[$key][tmp_name], $add);
chmod("$add",0644);$new_pic = "$propname+$key.jpg";
$new_image = "$uploaddir$new_pic";$sourcefile = "$uploaddir$new_pic";
$picsize = getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];if ($source_x > $source_y)
{
$percentage = ($target / $source_x);
} else {
$percentage = ($target / $source_y);
}
$dest_x = round($source_x * $percentage);
$dest_y = round($source_y * $percentage);$targetfile = "$uploaddir".$propname."-".$key.".jpg";
$jpegqual = 90;
$source_id = imagecreatefromjpeg("$sourcefile");
$target_id = imagecreatetruecolor($dest_x, $dest_y);
$target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);
imagejpeg($target_id,"$targetfile",$jpegqual);
unlink("$new_image");
}
}
Hope it helps.