0

I have a MySQL db with a table, that has the following field:

txtProductBowlImage: example " C:\BowlPhotos\Thumbs\MapleTi07-030tmb.jpg"

I want to use the data in that field to produce an image that I can print to a text file.

I read the data then try to echo it to the screen...and I see a small icon, but no image.

Here's the code:

    <?php
 $con = mysql_connect("localhost","xxxxxxx","zzzzzzzzzzzzzzz");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("scwdb", $con);

$result = mysql_query("SELECT * FROM tblsplintersbowlinventory WHERE txtVenue = 'Shoreline'");

while($row = mysql_fetch_array($result))
  {
  echo $row['intProductID'] . " " . $row['txtProductBowlCode'] . " " . $row['txtProductBowlImage'] . "]";
  echo "<br />";
  echo "<br />";
  echo 'txtProductBowlImage: ' . $row['txtProductBowlImage'];
  echo "<br />";
  $img = $row['txtProductBowlImage'];
  echo '...............$img: ' . $img;
  echo "<br />";
  echo "<img src=" . $img . ">";
  echo "<img src='C://BowlPhotos//Thumbs//Ash07-013_btmb.jpg'>";
  echo "<img src='Ash07-013_btmb.jpg'>";
  echo "<br />";
  echo "==================";
  echo "<br />";
  }

mysql_close($con);
?> 

Note that I try 3 times to show an image: 1: using the url from the db and putting it in an echo statement this url uses the '\' separater in the data. 2: imbedding a sample of the actual text path and filename in an echo statement this url uses the '/' separater in the url. 3: imbedding only the filename, no path, in an echo statement

When I run this, I get the following output:

enter image description here

Echo #1 & #2 produce the icon, echo #3 shows a local copy of the image in the same folder as the php.

From this it appears that it is the path that is not being used, either with the '\' or '/' separater.

I assume the '\' may be seen as an escape, but why doesn't the url with the '/' work?

Stephen
  • 155
  • 2
  • 16

3 Answers3

1

All below is wrong seeing as "Linking to local resources is disabled in all modern browsers due to security restrictions"

To link to a local image you use the format

<img src='file:///C:/path/to/image/pic.jpg'>

You can replace the backslashes with forward slashes using

$img = str_replace('\\', '/', $row['txtProductBowlImage']);

Now you can add the file:/// and the single quotes around the path like so

echo "<img src='file:///" . $img . "'>";

With double quotes you could skip the dot operator to append strings and use

echo "<img src='file:///$img'>";

The dollar $img will be recognized as a variable

Community
  • 1
  • 1
  • I see that - made the change but it still doesn't show the image, just the icon. I even tried echoing the HTML you show, that does the same thing... – Stephen Nov 05 '12 at 01:40
  • Try adding`file:///` before the path. So it's But, you do know that the path is of a local file? It is only visible to you who has the image in that location. – DrJonOsterman Nov 05 '12 at 01:51
  • That doesn't work either... and this will be a local script, I will use it to print ID cards for each bowl. – Stephen Nov 05 '12 at 01:59
  • Did you note that you have to replace backslashes with forward slashes? You can do it with the str_replace function. $img = str_replace('\\', '/', $img); You can do it in one line with $img = str_replace('\\', '/', $row['txtProductBowlImage']); – DrJonOsterman Nov 05 '12 at 02:03
  • Yeah, I knew the "\" was part of the problem and I need to make that change - I was trying it both ways since neither worked...I just added that to the code, and ran it. The change is made, but it still just gives me the icon. – Stephen Nov 05 '12 at 02:09
  • What does the broken icon HTML look like? I can see it just fine in all browsers with `` given that the image exists there of course – DrJonOsterman Nov 05 '12 at 02:14
  • Looking at the source I see this: 354 MapleTi07-030 C:\BowlPhotos\Thumbs\MapleTi07-030tmb.jpg]

    txtProductBowlImage: C:\BowlPhotos\Thumbs\MapleTi07-030tmb.jpg
    ...............$img: C:/BowlPhotos/Thumbs/MapleTi07-030tmb.jpg

    ==================
    – Stephen Nov 05 '12 at 02:20
  • And yes, I just checked, the file:///C:\BowlPhotos\Thumbs\MapleTi07-030tmb.jpg command does bring up the actual image. Make that file:///C:/BowlPhotos/Thumbs/MapleTi07-030tmb.jpg...:) – Stephen Nov 05 '12 at 02:22
  • Delete all the other attempts at printing the image except the one with `file:///` and the forward slashes, since they won't display anyway. – DrJonOsterman Nov 05 '12 at 02:26
  • OK, that gives me $img = $row['txtProductBowlImage']; $img = str_replace('\\', '/', $img); echo ""; And the same result. – Stephen Nov 05 '12 at 02:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19061/discussion-between-stephen-and-jonosterman) – Stephen Nov 05 '12 at 02:39
0

It seams your trying to get a file from your local computer. So you have to run a webserver that hadle php

You might be trying to read an image where the server dont have acess.

try to place the file with your html or php file

MadeInDreams
  • 1,991
  • 5
  • 33
  • 64
0

OK, with JonOsterman's greatly appreciated aid, the final working code is:

    <?php
$con = mysql_connect("localhost","root","Madge1938");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("scwdb", $con);

$result = mysql_query("SELECT * FROM tblsplintersbowlinventory WHERE txtVenue = 'Shoreline'");

while($row = mysql_fetch_array($result))
{
echo $row;
echo "<span>" . $row['intProductID'] . " " . $row['txtProductBowlCode'] . " " . $row['txtProductBowlImage'] . "</span>";
echo "<br />";
$img = $row['txtProductBowlImage'];
echo "<span> Original img: " . $img . " </span>";
$img = str_replace('\\', '/', $img);
echo "<br />";
$img = str_replace('C:/BowlPhotos/Thumbs/', 'Thumbs/', $img);
echo "<span>Folder img: " . $img . " </span>";
echo '<img src="' . $img . '">';
echo "<br />";
$img = str_replace('Thumbs/', '', $img);
echo "<span>Local img: " . $img . " </span>";
echo '<img src="' . $img . '">';
echo "<br />";
echo "<span>done</span><br />";
}

mysql_close($con);
?

There were a number of changes, but the biggest surprise was that the php could not open a folder not in the same folder or a sub folder of the php file! When I moved the entire folder to a sub of the php folder, the code above runs!

Also, as seen above, the HTML does not need a "file:///" for a local file...

And I should mention, this is intended to run only on my local server - not on the web. It is just for my own use to keep track of the wood bowls I make and sell.

Again, many thanks to Jon...

Stephen
  • 155
  • 2
  • 16