0

I am trying to display an image in a table from a query like this:

echo ("<td> <img src=" . $row['img1'] . "></td>");

This works until there is a space in the saved data. I am having the problem with the data entry refimg\AMYLO A-1.jpg

It works with data that looks like this: refimg\BCL-2-1.jpg

Any way to fix this?

Jens
  • 67,715
  • 15
  • 98
  • 113
Remco
  • 1

1 Answers1

1

use urlencode to encode special character:

echo ("<td> <img src=" . urlencode($row['img1']) . "></td>");
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Would quoting the content of the src attribute be enough? – JValker Feb 07 '20 at 10:51
  • 1
    urlencode seems to be the right track but we can't encode the entire URL but rather just the file name https://3v4l.org/MJmkE – nice_dev Feb 07 '20 at 10:53
  • 1
    @mickmackusa it sounds like `refimg\AMYLO A-1.jpg` was their input data here, and in that case vivek would be correct, that the whole path should probably not be URL-encoded as one single thing. – 04FS Feb 07 '20 at 11:00
  • Ah, yes, the slash. @04FS I added another dupe to the list. (Probably more robust as a DIRECTORY SEPERATOR) – mickmackusa Feb 07 '20 at 11:01
  • If I use the urlencode all my images wont be displayed instead of just the ones with a space in it. – Remco Feb 07 '20 at 11:02
  • 1
    @Remco yes, you will have to split your path first, so that the \ does _not_ get encoded as well. (Btw., rather bad to have a \ in your paths to begin with, URLs should always use /.) – 04FS Feb 07 '20 at 11:03
  • the source is on a local server folder named refimg – Remco Feb 07 '20 at 11:03
  • Thanx ill try some of the options mentioned! The original code is not mine so I hope I don't have to change everything. – Remco Feb 07 '20 at 11:20