displaying images from a folder with path stored in mysql
Posted: Mon Oct 12, 2009 10:47 am
hi all
I had been trying to store and display all images from mysql earlier but was unsuccessful. now i am trying to do this by uploading an image to a folder and storing its path in mysql table.I have managed to store the image in a folder(C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/) and path in mysql with coloumn name "path".I have also given a id to each path. Now i have created a display.html with text field with name "id" and i want to display that corresponding image on the page or i want to display all the images in the folder using their path thats stored in mysql..
heres my codes
upload.php
<?php
$uploadDir = 'C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
mysql_connect("localhost", "root", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload3 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br>Files uploaded<br>";
}
?>
this upload.php works fine
heres my display.php using "id"
f(isset($_GET['id']))
{
include '../library/config.php';
include '../library/opendb.php';
$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);
header("Content-type: $type");
echo"<img src='$filePath'/>";
include '../library/closedb.php';
exit;
this doent work
also i tried to display all images
<?php
$dir = 'C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/';
$sAltText = "Picture";
foreach (glob("*.jpg") as $file) {
echo "file: $file<br />\n";
echo "<i>filename:</i> <b>$file</b>, <i>filetype:</i> <b>" . filetype($file) . "</b><br />\n";
echo '<img src="' . $file . '" border="0" alt="$sAltText" /><br />' . "\n";
}
?>
<?php
mysql_connect("localhost", "root", "ryan") or die(mysql_error());
mysql_select_db("ryan") or die(mysql_error());
$result = mysql_query("SELECT * FROM upload3");
While($row = mysql_fetch_array($result)){
echo"<img src='C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/". $row['name'] . "' />\n";
this gives me blank images or missing pic links as if the path to the image is wrong..
thanks
I had been trying to store and display all images from mysql earlier but was unsuccessful. now i am trying to do this by uploading an image to a folder and storing its path in mysql table.I have managed to store the image in a folder(C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/) and path in mysql with coloumn name "path".I have also given a id to each path. Now i have created a display.html with text field with name "id" and i want to display that corresponding image on the page or i want to display all the images in the folder using their path thats stored in mysql..
heres my codes
upload.php
<?php
$uploadDir = 'C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
mysql_connect("localhost", "root", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload3 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br>Files uploaded<br>";
}
?>
this upload.php works fine
heres my display.php using "id"
f(isset($_GET['id']))
{
include '../library/config.php';
include '../library/opendb.php';
$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);
header("Content-type: $type");
echo"<img src='$filePath'/>";
include '../library/closedb.php';
exit;
this doent work
also i tried to display all images
<?php
$dir = 'C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/';
$sAltText = "Picture";
foreach (glob("*.jpg") as $file) {
echo "file: $file<br />\n";
echo "<i>filename:</i> <b>$file</b>, <i>filetype:</i> <b>" . filetype($file) . "</b><br />\n";
echo '<img src="' . $file . '" border="0" alt="$sAltText" /><br />' . "\n";
}
?>
<?php
mysql_connect("localhost", "root", "ryan") or die(mysql_error());
mysql_select_db("ryan") or die(mysql_error());
$result = mysql_query("SELECT * FROM upload3");
While($row = mysql_fetch_array($result)){
echo"<img src='C:/Documents and Settings/Administrator/Desktop/storing-image-folder/images/images1/". $row['name'] . "' />\n";
this gives me blank images or missing pic links as if the path to the image is wrong..
thanks