Page 1 of 1

displaying images from a folder with path stored in mysql

Posted: Mon Oct 12, 2009 10:47 am
by ryanfern86goa
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

Re: displaying images from a folder with path stored in mysql

Posted: Mon Oct 12, 2009 11:01 am
by Weiry
Please see my signature about posting code.
Please wrap your code in the code or php tags.
===========================================================

If you are storing images as a link, why are you trying to display them as a glob?
If you want to store the link, why not use the link as the reference?

Code: Select all

$id = $_GET['id'];
$query = "SELECT `fileName`, `path` FROM `upload2` WHERE `id` = '{$id}'";
$result = mysql_query($query) or die('Error, query failed');
$imageArray = array();
while($row = mysql_fetch_assoc($result)){
   print "<img src='".$row['path'].$row['fileName']."'/>";
}
 
And when inserting, the only information you need, assuming your linking through the PATH to the file only, is the id, filename and the path. Where filename would be "myPicture.jpg" and the path would be "C:/myDirectory/images/"
ryanfern86goa wrote:this gives me blank images or missing pic links as if the path to the image is wrong..
Answer is here:
ryanfern86goa wrote:img src='C:/Documents and Settings/
avoid using spaces whenever linking to a file, HTML doesnt recognize this very well and you need to use %20 to replace your spaces.

Re: displaying images from a folder with path stored in mysql

Posted: Mon Oct 12, 2009 2:10 pm
by ryanfern86goa
hi Weiry

thanks for your reply. i did wat you said...
i changed my upload folder to C:/pics/keith.jpg so dat it doesnt have any spaces

and replaced my display.php with urs but since my path stores the entire path plus the image name.. i did this
print "<img src='".$row['path']."'/>";
also tried wat u suggested
print "<img src='".$row['path'].$row['fileName']."'/>";

but i still get the blank or missing pic icon

any suggestions

thanks