href syntax : is it okay to have space in file name

HtmlSyntaxAnchorHref

Html Problem Overview


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" />

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

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.

Solution 3 - Html

> 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);

Solution 4 - Html

<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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMoonView Question on Stackoverflow
Solution 1 - HtmlFrédéric HamidiView Answer on Stackoverflow
Solution 2 - HtmlCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 3 - Htmlashish bhattView Answer on Stackoverflow
Solution 4 - HtmlKarthik RatnamView Answer on Stackoverflow