【问题标题】:uploaded file oppening error上传文件打开错误
【发布时间】:2023-03-19 21:46:01
【问题描述】:

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

我可以这样看他们:

打开文件号 1 后文件完美打开,但问题是当我触摸文件 2 时 因为这个文件名字里有空格或者特殊符号,打开是这样的

这是我的 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.";
}
}
?> 

显示.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);
?>

【问题讨论】:

  • 如果你需要帮助,你需要把你的代码放在这里。
  • 请以文本形式发布代码
  • urlencode() 是要走的路!如果名称中有空格

标签: php upload


【解决方案1】:

问题出在你的show.php这里:

<a href='$path$result'>

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

使用htmlspecialchars() 来避免这个问题。

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

【讨论】:

猜你喜欢
  • 2021-10-16
  • 2014-06-20
  • 1970-01-01
  • 2012-05-23
  • 2015-12-18
  • 2014-09-23
  • 2015-08-22
  • 2011-10-19
  • 1970-01-01
相关资源
最近更新 更多