Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

mysql

  • This topic is locked This topic is locked
213 replies to this topic

#25 billjones

billjones

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 13 October 2008 - 09:56 AM

can anyone help with the above?
  • 0

#26 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 13 October 2008 - 10:37 AM

Umm.. do you use the same script or you have changed it..?

If you have changed it, please post it here so I can check what's the problem
  • 0

#27 billjones

billjones

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 14 October 2008 - 05:24 AM

Hi Jann,

Thanks for the quick reply, here is my code.


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>add pic</title>
</head>
<body>
<form action="insert.php" method="post" enctype="multipart/form-data" name="changer" >
<input name1="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpg" type="file">
<input value="Submit" type="submit">
</form>
</body>
</html>


php code for the page insert.php


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>insert</title>
</head>
<?php

// Create MySQL login values and
// set them to your login information.
$username = "abc";
$password = "123";
$host = "localhost";
$database = "test";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);


// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


// Create the query and insert
// into our database.
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);

// Print results
print "Thank you, your file has been uploaded.";

}
else {
print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?>

<body>
</body>
</html>


this works fine untill i try to get the pic out of the db......

php code for print_pic.php


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php

$username = "abc";
$password = "123";
$host = "localhost";
$database = "test";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

//$id = $_GET['id'];


$id = 1; // just a test id to see if it works!

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;

}

?>
</body>
</html>


I hope you can help.

Thanks Bill.

Edited by Jordan, 09 November 2008 - 06:18 PM.
added tags

  • 0

#28 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 09 November 2008 - 06:14 PM

please use the php tags.

this is a special php file when you output the picture, it may not contain any html as it's a picture you're returning, not a web page.

remove everything before <?php and after ?> and it will work. make extremely sure that there are absolutely no letters, spaces or any other symbols before the <?php otherwise, the system might believe it's a web page and goes nuts.
  • 0

#29 anujphp

anujphp

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 19 November 2008 - 01:47 PM

hello
thanks for the code its works fine for me but i have a problem..though the script work fine when i open my page in flock or mozilla or any toher browser but it dont work when i open the page in internet explorer..it try to download the php file....can you kindly solve my this problem....
i am trying to open the file with this url
localhost/test/displayimage.php=?1



IT works fine with in flock and mozilla
  • 0

#30 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 20 November 2008 - 11:20 AM

your link must be..

displayimage.php?id=1
  • 0

#31 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 20 November 2008 - 12:58 PM

The ? indicates the GET arguments. Then you have name/value pairs, joined with the = symbol and separated from other pairs via the & symbol.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#32 sanyal

sanyal

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 16 December 2008 - 07:10 AM

hi.......
i am new to php...i like u r tutorial....its simple easy going....
i tried u r example of Tutorial: Storing Images in MySQL with PHP / Part II / Display your images , but i am getting this error....somthing like this


Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\task\show_img.php:9) in C:\xampp\htdocs\task\show_img.php on line 30

and here is my code


<?php

$username = "root";
$password = "";
$host = "localhost";
$database = "test";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

//$id = $_GET['id'];
$id=2;
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['img'];

header('Content-type: image/jpg'); =====error =====> ( line 30)
echo $content;

}

?>


thanks in advance.....waiting for the response..!

Edited by Jaan, 16 December 2008 - 07:39 AM.
Please use code tags when you're posting your codes!

  • 0

#33 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 16 December 2008 - 01:56 PM

This error means you have already sent your headers somewhere. Do you have a space before or above <?php ? Is another script being executed first?
  • 0

#34 sanyal

sanyal

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 17 December 2008 - 01:06 AM

first of all thanks for u r .responce.........

this is the whole code..........
<html>
<head>
<title>Untitled Document</title>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> -->
</head>

<body>
<?php

$username = "root";
$password = "";
$host = "localhost";
$database = "test";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

//$id = $_GET['id'];
$id=4;
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$query = mysql_query("SELECT * FROM images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['img'];

header('Content-type: image/jpg');
echo $content;

}

?> 
</body>
</html>


.........i have commented that line as u said it might have already declared ..........
now i am not getting the error.....but the out put...is somthing like this........

ÿØÿà�JFIF������ÿÛ�C� $.' ",#(7),01444'9=82<.342ÿÛ�C 2!!22222222222222222222222222222222222222222222222222ÿÀ�§4"�ÿÄ����������� ÿÄ�µ���}�!1AQa"q2‘¡#B±ÁRÑð$3br‚ ........and so ...on
can u plzss...tell me what would b the problem.........and y is it happening so....!
thanks in advance.......

Edited by Jaan, 17 December 2008 - 07:51 AM.
Please use code tags when you're posting your codes!

  • 0

#35 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 17 December 2008 - 02:39 AM

As mentioned one page back in this thread, ABSOLUTELY NOTHING can be put before the "<?php" tag or after the "?>" for this script! It shall NOT have any HTML or HEAD or BODY, as it is NOT an Webpage, this is an image file you want to show.

http://forum.codecal....html#post95788
  • 0

#36 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 17 December 2008 - 07:52 AM

you're right orjan.. You MUST NOT have anything after and before PHP tags!
  • 0