php 警告 fclose() 期望参数 1 为资源,第 60 行 /web/htdocs/www.mysite.org/home/DDL/download.php 中给出的布尔值
php warning fclose() expects parameter 1 to be resource, boolean given in /web/htdocs/www.mysite.org/home/DDL/download.php on line 60
我拿起并修改了一个 PHP 下载代码,但我总是得到这个错误:
Warning: fclose() expects parameter 1 to be resource, boolean given in /web/htdocs/www.mysite.org/home/DDL/download.php on line 60
显然 "www.mysite.org" 与我的网站 url 发生了变化。
我在其他问题里搜了一下,都是类似的问题,不完全相同,情况不同。
脚本是这个:
<?php
ignore_user_abort(true);
set_time_limit(0);
$path = "/DDL/FILES/";
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']);
$dl_file2 = filter_var($dl_file, FILTER_SANITIZE_URL);
$fullPath = $path.$dl_file2;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "zip":
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "mkv-video":
header("Content-type: video/x-matroska");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "mp4-video":
header("Content-type: video/mp4");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "aac":
header("Content-type: audio/aac");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "flac":
header("Content-type: audio/x-flac");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "mp3":
header("Content-type: audio/mpeg");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
ob_clean();
flush();
fclose ($fd);
} else {
echo 'something wrong with the file?';
}
exit;
我做了一些研究并与一些人交谈,我们得出的结论是可能是数据库错误,因为我们尝试的所有方法都没有解决问题。
谁能帮我解决这个错误?
提前致谢。
让我们从以下字符串开始:
if ($fd = fopen ($fullPath, "r")) {
"What is wrong with it?" 你可能会问。好吧,如果 fopen
returns FALSE
,你的表达式不会被评估,你最终会直接跳到
ob_clean();
flush();
fclose ($fd);
并且由于 $fd
未在此范围内定义...您现在应该已经猜到了。
将代码移到 if
语句内的 if
语句之后:
if ($fd = fopen ($fullPath, "r")) {
// the rest of the code that could have been cropped
ob_clean();
flush();
fclose ($fd);
} else {
echo 'something wrong with the file?';
}
exit;
尝试像下面这样更简单的方法:
if (($fd = fopen($file, "a")) !== false) {
fwrite($fd, 'message to be written' . "\n");
fclose($fd);
}
然后尝试将其集成到您的脚本中。
我拿起并修改了一个 PHP 下载代码,但我总是得到这个错误:
Warning: fclose() expects parameter 1 to be resource, boolean given in /web/htdocs/www.mysite.org/home/DDL/download.php on line 60
显然 "www.mysite.org" 与我的网站 url 发生了变化。
我在其他问题里搜了一下,都是类似的问题,不完全相同,情况不同。
脚本是这个:
<?php
ignore_user_abort(true);
set_time_limit(0);
$path = "/DDL/FILES/";
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']);
$dl_file2 = filter_var($dl_file, FILTER_SANITIZE_URL);
$fullPath = $path.$dl_file2;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "zip":
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "mkv-video":
header("Content-type: video/x-matroska");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "mp4-video":
header("Content-type: video/mp4");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "aac":
header("Content-type: audio/aac");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "flac":
header("Content-type: audio/x-flac");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "mp3":
header("Content-type: audio/mpeg");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
ob_clean();
flush();
fclose ($fd);
} else {
echo 'something wrong with the file?';
}
exit;
我做了一些研究并与一些人交谈,我们得出的结论是可能是数据库错误,因为我们尝试的所有方法都没有解决问题。
谁能帮我解决这个错误?
提前致谢。
让我们从以下字符串开始:
if ($fd = fopen ($fullPath, "r")) {
"What is wrong with it?" 你可能会问。好吧,如果 fopen
returns FALSE
,你的表达式不会被评估,你最终会直接跳到
ob_clean();
flush();
fclose ($fd);
并且由于 $fd
未在此范围内定义...您现在应该已经猜到了。
将代码移到 if
语句内的 if
语句之后:
if ($fd = fopen ($fullPath, "r")) {
// the rest of the code that could have been cropped
ob_clean();
flush();
fclose ($fd);
} else {
echo 'something wrong with the file?';
}
exit;
尝试像下面这样更简单的方法:
if (($fd = fopen($file, "a")) !== false) {
fwrite($fd, 'message to be written' . "\n");
fclose($fd);
}
然后尝试将其集成到您的脚本中。