Forum Moderators: coopster

Message Too Old, No Replies

upload and display image

         

Yuen

11:19 am on Jun 9, 2009 (gmt 0)

10+ Year Member



hi
i try to do upload image in php,
this code i can insert image to database,
but can not display the image ,
any one can help?


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php
include("connection.php");
$id = 1;
$image = mysql_query("SELECT * FROM picture WHERE picid='".$id."'");
$row = mysql_fetch_array($image);

echo $row['pic'];
header("Content-type: image/jpeg");
echo '<img src="'.mysql_result($row, 0).'">';

?>

<FORM ENCTYPE="multipart/form-data" ACTION="" METHOD=POST>
Upload this file: <INPUT NAME="image" TYPE="file">
<INPUT TYPE="submit" VALUE="Send File" name="sub"></FORM>

</body>
</html>

<?php

if (isset($_POST['sub']) ) {

// 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($content);

fclose($fp);

// Create the query and insert

// into database.

$query = "INSERT INTO picture (pic) VALUES ('$data')";

$results = mysql_query($query);

// Print results

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

}

else {

print "No image selected/uploaded";

}

?>

abidshahzad4u

2:05 pm on Jun 9, 2009 (gmt 0)

10+ Year Member



I think this is not good practice. Just save the path of the file with file name and move the file to some folder. That will be best.