I was writing a simple php page to list the files in the directory and generate hyperlink to download them. It should be a very easy job, but something went wrong, as always. The escape characters cause the hyperlink to be cut in half and the link was broken. So to fix that, those characters should be replaced before been send to the httpd. So here it is:
$m_fr = str_replace("%","%25","$m_fr"); // % -> First!!!
$m_fr = str_replace(" ","%20","$m_fr");
$m_fr = str_replace("#","%23","$m_fr");
$m_fr = str_replace("&","%26","$m_fr");
$m_fr = str_replace("+","%2B","$m_fr");
$m_fr = str_replace("/","%2F","$m_fr");
$m_fr = str_replace("=","%3D","$m_fr");
$m_fr = str_replace("?","%3F","$m_fr");
And the whole page:
<!-------------------------------------
| List Dir Funtion w. Upload |
| Version: 1.0.04 |
| By: San |
| s(at)zhujunsan.net |
-------------------------------------->
<html><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>List</title>
<body>
<?php
if ($_FILES)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: ".$_FILES["file"]["error"]."<br />";
}
else
{
echo "Upload: ".$_FILES["file"]["name"]."<br />";
echo "Type: ".$_FILES["file"]["type"] . "<br />";
echo "Size: ".(round($_FILES["file"]["size"]/1024, 2)).
"kb(". ($_FILES["file"]["size"] ) .")<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"./".$_FILES["file"]["name"]);
echo "Files stored: ".$_FILES["file"]["name"]."<p>";
}
}
}
if($_GET["upload"]=="Password"||0) //Change Password to whatever you like. Be aware this is still unsafe, just preliminary protect.
{
echo "<form action=\"list.html\" method=\"post\"
enctype=\"multipart/form-data\">
<label for=\"file\">Filename:</label>
<input type=\"file\" name=\"file\" id=\"file\" /><br />
<input type=\"submit\" name=\"submit\" value=\"Submit\" />
</form>";
}
function tree($folder) {
$mydir = dir($folder);
while($file = $mydir->read()) {
if((is_dir("$folder/$file")) && ($file!==".") && ($file!==".."))
{
// tree("$folder/$file");
}else {
if(($file!==".")&&($file!=="..")&&($file!=="index.html")&&($file!=="favicon.ico")&&($file!=="robots.txt")) {
$m_f=str_replace("./","","$folder/$file");
$m_fr = $m_f;
$m_fr = str_replace("%","%25","$m_fr"); // % -> First!!!
$m_fr = str_replace(" ","%20","$m_fr");
$m_fr = str_replace("#","%23","$m_fr");
$m_fr = str_replace("&","%26","$m_fr");
$m_fr = str_replace("+","%2B","$m_fr");
$m_fr = str_replace("/","%2F","$m_fr");
$m_fr = str_replace("=","%3D","$m_fr");
$m_fr = str_replace("?","%3F","$m_fr");
echo "<a href=$m_fr>$m_f</a><br />\r\n";
}
}
}
$mydir->close();
}
?>
<?php tree(".");?>
</body>
</html>

