55

it has always been my practice that when ever i use images i name them like walls_ico , bu_hover

so when i give paths they go like

<img src="images/walls_ico.ico" />
<img src="buttons/bu_hover.png" />

UNTIL now when i am on a project where users upload files...

i was wondering is it okay to have spaces between file and folders name like

<img src="buttons/bu hover.png" />
Rob
  • 14,746
  • 28
  • 47
  • 65
Moon
  • 19,518
  • 56
  • 138
  • 200
  • 2
    **Related:** [In a URL, should spaces be encoded using %20 or +?](http://stackoverflow.com/q/1211229/1497596) – DavidRR Sep 17 '14 at 13:12

4 Answers4

72

The src attribute should contain a valid URL. Since space characters are not allowed in URLs, you have to encode them.

You can write:

<img src="buttons/bu%20hover.png" />

But not:

<img src="buttons/bu+hover.png" />

Because, as DavidRR rightfully points out in his comment, encoding space characters as + is only valid in the query string portion of an URL, not in the path itself.

Community
  • 1
  • 1
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • 2
    Basically just url encode the src, if you are working with php just use: http://php.net/manual/en/function.urlencode.php – Bjarke Freund-Hansen Nov 13 '10 at 12:56
  • I knew I should've posted my answer before checking the facts... =/ my since I said pretty much the same thing I've deleted my answer. +1 for your speed, sir! =b – David Thomas Nov 13 '10 at 12:57
  • 1
    **Caution:** The [W3C Markup Validation Service](http://validator.w3.org/) did not flag occurrences of `a/@href` values with spaces in XHTML Transitional files that I checked. – DavidRR Sep 16 '14 at 18:43
  • 3
    I do not believe that `src="buttons/bu+hover.png"` is valid. According to [this answer](http://stackoverflow.com/a/497991/1497596) and [this answer](http://stackoverflow.com/a/1211256/1497596), encoding a space as a `+` is valid *only* in the **query string** portion of a URL. Furthermore, in tests I have just conducted, IE and Firefox both return 404 errors when I attempt to access a file where spaces in its name are (improperly) encoded as `+` characters. – DavidRR Sep 17 '14 at 13:09
  • 1
    But it says that this apply on for domain names. It should be ok to have spaces in src. – qfactor77 Mar 02 '18 at 11:10
5

Quoting HTML5 to back Frederic that spaces are not allowed:

http://www.w3.org/TR/html5/links.html#attr-hyperlink-href:

The href attribute on a and area elements must have a value that is a valid URL potentially surrounded by spaces.

The definition of "valid URL" points to: http://url.spec.whatwg.org which defines URL code points https://url.spec.whatwg.org/#url-code-points:

The URL code points are ASCII alphanumeric, "!", "$", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", ":", ";", "=", "?", "@", "_", "~", and code points in the ranges U+00A0 to U+D7FF, U+E000 to U+FDCF, U+FDF0 to U+FFFD, U+10000 to U+1FFFD, U+20000 to U+2FFFD, U+30000 to U+3FFFD, U+40000 to U+4FFFD, U+50000 to U+5FFFD, U+60000 to U+6FFFD, U+70000 to U+7FFFD, U+80000 to U+8FFFD, U+90000 to U+9FFFD, U+A0000 to U+AFFFD, U+B0000 to U+BFFFD, U+C0000 to U+CFFFD, U+D0000 to U+DFFFD, U+E1000 to U+EFFFD, U+F0000 to U+FFFFD, U+100000 to U+10FFFD.

The spec then uses the term URL code points on various parts of the parsing algorithm as:

If c is not the EOF code point, not a URL code point, and not "%", parse error.

for the scheme, authority, relative path, query state and fragment states: so the entire URL.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
3

If you are using PHP

then find out this code

$result = mysqli_query($con,$sql);
    //echo $ip."<br />";REGEXP
    //echo $name."<br />";
    echo "<table border=2px style='border-radius=20px;' align=center><tr>
    <th>Document ID</th>
    <th>Document Name Type</th>
    <th>Download Documents</th>
    </tr>";//<th>Project Document Type</th>
    while($row = mysqli_fetch_array($result)) {
        $path1=$row['FOLDERNAME'] .'/'. $row['FILENAME'] .'.'. $row['DOCTYPE'];
        $path=str_replace(" ", '%20', $path1);
        echo "<tr>";
        echo "<td>" . $row['DocID'] . "</td>";
       // echo "<td>" . $row['PROJDOCTYPE'] . "</td>";Thank you. Your Apple ID is now ready for use.
        echo "<td>" . $row['DOCNAME'] . "</td>";
        echo '<td><a href=Tender/'.$path.'>'.$row['DOCNAME'].'</a></td>';
        echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
ashish bhatt
  • 3,091
  • 5
  • 25
  • 23
-3
<body>
<img src="file:///C|/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Water lilies.jpg"
</body>

spaces will be allowed only when you are working in local hosts

Karthik Ratnam
  • 3,032
  • 2
  • 20
  • 25
  • 4
    They're not actually allowed, what may happen to work in one case will break depending on browsers and doctypes. – Tobu Oct 18 '12 at 18:59