Cool Compiler
Cool Compiler

Reputation: 857

Adding Dynamic link with Spaces in variable

i am trying to create a dynamic link. $path="uploads/" and $fileName="Data Communication and Networking.pdf" which is retrieved from database. But the link gets created with href="upload/Data" ignoring the " Communication and Networking.pdf" part. How to add $fileName with spaces between the content.

$message=$row["message"];       
$fileName=$row["filename"];
$date=$row["date"];   

echo "<tr>";
echo "<td>".$Serial."</td>";
echo "<td>".$message."</td>";

echo "<td><a href=".$path.$fileName.">Download</a></td>";
echo "<td>".$date."</td>";
echo "</tr>";

$Serial++;;

Upvotes: 2

Views: 513

Answers (3)

dalior
dalior

Reputation: 1

$path="/path";
$filename="/file with space.name";
echo "<td><a href=".$path.$fileName.">Download</a></td>";

Will output:

`<td><a href=/path/file with space.name>Download</a></td>`

So you need to put the path between the quotes like this:

$path="/path";
$filename="/file with space.name";
echo "<td><a href='".$path.$fileName."'>Download</a></td>";

Upvotes: 0

Cid
Cid

Reputation: 15257

You can replace any space with %20

$CompletePath = str_replace(" ", "%20", $path . $fileName);
echo "<td><a href=" . $CompletePath . ">Download</a></td>";
//uploads/Data%20Communication%20and%20Networking.pdf is a valid URL

Upvotes: 1

B. Desai
B. Desai

Reputation: 16436

href value must consist in quotes. Place single quotes around your value.change your href as below:

echo "<td><a href='".$path.$fileName."'>Download</a></td>";

Upvotes: 2

Related Questions