哪里出错了?
<?
if($_GET['data'])
{
print 'atmam';
include ('http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1');
}
else {
print 'fail to download'; }
?>
屏幕上写有错误:
Warning: Unexpected character in input: '' (ASCII=1) state=1 in http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1 on line 515
Parse error: syntax error, unexpected T_STRING in http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1 on line 515
PS:http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1 =直接文件下载链接
你能帮上忙吗?诚挚的问候
发布于 2011-11-26 18:28:32
看看这是否能达到您想要的效果:
<?php
if ($_GET['data']) {
// Some headers that indicate a generic download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment');
// Try and read the file directly to the client
if (!@readfile('http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1')) {
// Try and clear the header and print a message. This may not work depending on the result of the readfile() attempt.
@header('Content-Disposition:');
print 'fail to download';
}
exit;
}
?>
另一种方法(许多人认为更好)是这样的:
<?php
if ($_GET['data']) {
// Redirect the client to the actual location
header('HTTP/1.1 302 Found');
header('Location: http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1');
exit;
}
?>
发布于 2011-11-26 19:12:33
PHP为此提供了一个名为file_exists的函数。$_GET'data‘用于从附加了name="data“的html元素中获取信息。如果你的文档中没有这个,脚本将永远不会运行,因为它正在寻找不存在的东西。即使该元素确实存在,这也不是$_GET的必要或推荐用法。
要了解更多关于您正在尝试执行的操作的信息,请查看此链接,并查看我的示例。
http://php.net/manual/en/function.file-exists.php
要使用它,只需执行以下操作:
$filename = 'http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1';
if (file_exists($filename)) {
echo 'atmam';
include ('$filename');
}
else
echo 'Failed to download file';
我假设你会在你的站点必须访问的任何文件中使用它,为了节省你的时间,你可以在函数中使用它。
function testfile(filename) {
if (file_exists(filename)) {
echo 'atmam';
include ('filename');
}
else
echo 'Failed to download file';
}
如下所示调用函数:
$filename1 = 'http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1';
$filename2 = 'something.txt';
function testfile($filename1);
使用该函数,您可以根据需要检查任意多个文件名,并为每个文件名使用变量。
编辑:要解决即将出现的语法错误,必须确保包含的文件没有错误。请把它贴在这里,让我们看看。删除回显和打印不会改变任何东西,事实上,您需要它们在其中以便进行调试。首先尝试使用我放在这里的一小段代码,这是检查文件是否存在的正确方法,如果存在,则执行一些操作。一旦您使用正确的代码检查文件并将其包含在内,那么您将确保一旦修复了包含文件中的任何问题,您将拥有所需的功能。
希望这能帮到你!-Sean
https://stackoverflow.com/questions/8280272
复制相似问题