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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

11 Comments

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...
Try addingfile:/// before the path. So it's <img src='file:///C:/BowlPhotos/Thumbs/MapleTi07-030tmb.jpg'> 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.
That doesn't work either... and this will be a local script, I will use it to print ID cards for each bowl.
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']);
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.
|
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

1 Comment

And use a path relative to your web server root directory not the hard drive
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.