上传文件oppening错误

时间:2014-12-24 16:51:31

标签: php upload

我刚创建了上传脚本,但上传文件后出现问题

我可以这样看:

enter image description here

打开文件编号1文件打开后,但问题是我触摸文件2 因为这个文件在名称中有空格或特殊符号,并且像这样打开

enter image description here

这是我的php文件

<?php

$target_dir = "uploads/up/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$FileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}


// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}




// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has 
been             uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}
?> 

show.php

<?php
$sub = ($_GET['dir']);
$path = 'up';
$path = $path . "$sub";
$dh = opendir($path);
$i=1;







while (($file = readdir($dh)) !==   false) {
if($file != "." && $file != "..") {
    if (substr($file, -4, -3) =="."){


echo "<a href='".htmlspecialchars($path$result)."'>".$file."</a><br>";

    }
    $i++;
}
}

closedir($dh);
?>

1 个答案:

答案 0 :(得分:0)

问题出在show.php这里:

<a href='$path$result'>

文件名中的'href参数中使用的相同,会打破它。

使用htmlspecialchars()来避免此问题。

echo "<a href='".htmlspecialchars($path.$result)."'>".$file."</a><br>";