Jump to content

Recommended Posts

Hello,

 

I have a situation regarding the displaying the image from MySQL database.

 

Here is my code of upload the images to MySQL.

 

<?php

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
	$fileName = $_FILES['userfile']['name'];
	$tmpName  = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];

	$fp      = fopen($tmpName, 'r');
	$content = fread($fp, filesize($tmpName));
	$content = addslashes($content);
	fclose($fp);

	if(!get_magic_quotes_gpc())
	{
		$fileName = addslashes($fileName);
	}

	$conn = mysql_connect("localhost", "root", "");
	mysql_select_db("test", $conn);

	$query = "INSERT INTO pics VALUES ('','$fileName', '$fileType', '$fileSize', '$content')";

	mysql_query($query, $conn) or die('Error, query failed');

	echo "<br>File $fileName uploaded<br>";
}

?>

<html>
<head>
<title>Pic Upload</title>
</head>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
	<tr>
		<td width="246">
		<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
		<input name="userfile" type="file" id="userfile">
	</td>
		<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
	</tr>
</table>
</form>
</html>

 

Here is my code for the displaying the images from MySQL.

 

        //echo "Hello ";
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("test");

$query = "SELECT content, type from pics where id = '1'";
$result = @MYSQL_QUERY($query);

$data = @MYSQL_RESULT($result,0,"content");
$type = @MYSQL_RESULT($result,0,"type");

header("Content-type: $type");
echo $data;

 

In this situation if I display the image without any string the it is displaying. But if I remove the comment from echo "Hello" then it is not working. I am trying to display the image in a <td></td> of table with info in other <td></td> with width amd height of <ing> tag specified.

 

Please let me know what is the solution regarding the situation. Thanks a lot in advance.

Link to comment
https://forums.phpfreaks.com/topic/196156-display-image-from-mysql/
Share on other sites

Each image displayed on a web page requires an <img src="URL_of_an_image" alt=""> tag - http://w3schools.com/html/html_images.asp This is basic html.

 

The URL_of_an_image that you put into the <img tag would be to your .php script that retrieves the image from the database and output the content-type header followed by the image data.

 

You cannot output image data directly on a web page.

You can not store images in Database.

First You need to create a new folder in your site root to store images.Then save the image name in Database and to display the image use the file name from DB

example:

define('GW_UPLOADPATH', 'images/');
fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$target = GW_UPLOADPATH . $tmpName;
move_uploaded_file($_FILES['userfile']['tmp_name'],  $target

$conn = mysql_connect("localhost", "root", "");
	mysql_select_db("test", $conn);

	$query = "INSERT INTO pics VALUES ('','$fileName', '$fileType', '$fileSize')";

	mysql_query($query, $conn) or die('Error, query failed');

	echo "<br>File $fileName uploaded<br>";
}

?>

Dstar, you can store images in the DB as well.

if($_FILES['file']['size'] > 0)
		{
			$fileName = $_FILES['file']['name'];
			$tmpName  = $_FILES['file']['tmp_name'];
			$fileSize = $_FILES['file']['size'];
			$fileType = $_FILES['file']['type'];

			$fp      = fopen($tmpName, 'r');
			$content = fread($fp, filesize($tmpName));
			$content = addslashes($content);
			fclose($fp);
			if(!get_magic_quotes_gpc())
			{
   				$fileName = addslashes($fileName);
			}
			$query = "INSERT INTO pictures (name, size, type, content ) ".
			"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

			mysql_query($query) or die('Error: Upload pictures failed.');
		}

 

then view the picture with

viewimage.php?id=1

if(isset($_GET['id'])) {
    $id=$_GET['id'];

    $query = "select content, type from pictures where id=$id";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);

       $data = $row['content'];
       $type = $row['type'];
      
      if ($type=="image/jpeg") $type = "jpeg";
      Header( "Content-type: $type");
      echo $data;
}

 

and you would show the image with <img src="viewimage.php?id=1" alt="" width="250" height="250">

and put the relevent data next to that image

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.