PHP&;MYSQL空白结果。脚本突然停止。无错误日志
我想编写一个简单的代码,让我根据URL中指定的ID下载文件。为此,它连接到数据库并尝试获取与ID关联的文件的信息。然后,它发送包含提取的信息和文件的标题 但由于我不知道的原因,它会输出一个空白页。任何地方都没有日志文件,只是它突然停止了 求你了,我非常感谢你的帮助。谢谢你花时间PHP&;MYSQL空白结果。脚本突然停止。无错误日志,php,mysql,Php,Mysql,我想编写一个简单的代码,让我根据URL中指定的ID下载文件。为此,它连接到数据库并尝试获取与ID关联的文件的信息。然后,它发送包含提取的信息和文件的标题 但由于我不知道的原因,它会输出一个空白页。任何地方都没有日志文件,只是它突然停止了 求你了,我非常感谢你的帮助。谢谢你花时间 <?php //We include the variables needed to do the connection to the db include '../php/vars.php'; $id=$_GET
<?php
//We include the variables needed to do the connection to the db
include '../php/vars.php';
$id=$_GET['id'];
//We connect to the database
$con = mysql_connect("$dbsv","$dbuser","$dbpass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("$dbname", $con);
// We do the query
if ($query === false) {echo mysql_error();}
$query = "SELECT name,filetype FROM links WHERE id=$id";
$result = mysql_query($query);
// Check result. This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysql_fetch_assoc($result))
{
// Assign variables
$filename = $row['name'];
$type = $row['filetype'];
}
//We download the file
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header( "Content-type: $type");
header('Content-Disposition: attachment; filename='.basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
fopen("$filename", "r");;
exit;
}
?>
我不是以英语为母语的。除了语法、语法和语法错误。如果您检测到它们,请通知我。我看到您想要传递文件,对吗
fopen("$filename", "r");
替换为
readfile($filename);
fopen只打开一个文件指针,对文件本身完全不做任何操作,您必须使用fread、write和fclose来处理文件,操作更多,但基本上fopen只给您一个文件指针有很多逻辑错误,您需要修复:
if ($query === false) {echo mysql_error();}
此时未设置$query
。所以它从来都不是假的。(您的意思基本上是if(null==false)
)
根据您的结果计数,您将多次重写$filename
,然后仅对最后一个值执行操作
- 将逻辑移动到
循环中while
- 或者,如果希望得到单个结果,则不要使用while循环
fread()
直到达到EOF
,然后使用fclose($handle)
bevor输出结果。
(这就是什么也看不见的原因)您可以使用readfile()代替。readfile()读取文件并将其写入输出缓冲区
这里是手动链接
在底部有一个语法错误:
fopen($filename,“$r”)代码>删除列表中的分号end@Bluedayz这不是语法错误<代码>代码>删除任何语句-并且“空”字符是有效语句。换句话说:这没关系:)readfile($filename)//fopen(“$filename”,“r”)代码> fOPEN不做您所期望的,使用RealFrime:MySqLi,MySqL**是不推荐的,或者您考虑使用PDO,也是一个伟大的。choice@Hanky웃不,它是空的。因此,使用==
执行检查将是正确的-但不使用==
执行检查。参见:if($a==false)回显“a为false”;如果($b==false)回显“b为false”代码>第一个条件为true,最后一个条件为true。我尝试删除while循环,但它停止工作,因此我想我将保持它不变。file\u get\u contents
也将不起任何作用,除非使用返回值,例如echo file\u get\u contents($filename)对于大文件,使用fread会更好,因为它将在完成第一个块后开始输出内容<代码>文件\u获取\u内容
阻塞,直到整个文件加载到内存中。是的,对,实际上我是说读取文件,我有点头晕更新我已将“fopen”改为“readfile”,效果非常好:D。非常感谢!!!!!
while ($row = mysql_fetch_assoc($result))
{
// Assign variables
$filename = $row['name'];
$type = $row['filetype'];
}
//We download the file
if (file_exists($filename)) {
fopen("$filename", "r");
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header( "Content-type: $type");
header('Content-Disposition: attachment; filename='.basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
}