首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >"include“中的"ifelse”命令

"include“中的"ifelse”命令
EN

Stack Overflow用户
提问于 2011-11-27 01:34:46
回答 2查看 96关注 0票数 0

哪里出错了?

代码语言:javascript
运行
复制
  <?
  if($_GET['data'])
  {
    print 'atmam';
include ('http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1');
  }
  else {
   print 'fail to download'; }
   ?>

屏幕上写有错误:

代码语言:javascript
运行
复制
    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 =直接文件下载链接

你能帮上忙吗?诚挚的问候

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-27 02:28:32

看看这是否能达到您想要的效果:

代码语言:javascript
运行
复制
<?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;

  }

?>

另一种方法(许多人认为更好)是这样的:

代码语言:javascript
运行
复制
<?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;

  }

?>
票数 0
EN

Stack Overflow用户

发布于 2011-11-27 03:12:33

PHP为此提供了一个名为file_exists的函数。$_GET'data‘用于从附加了name="data“的html元素中获取信息。如果你的文档中没有这个,脚本将永远不会运行,因为它正在寻找不存在的东西。即使该元素确实存在,这也不是$_GET的必要或推荐用法。

要了解更多关于您正在尝试执行的操作的信息,请查看此链接,并查看我的示例。

http://php.net/manual/en/function.file-exists.php

要使用它,只需执行以下操作:

代码语言:javascript
运行
复制
    $filename = 'http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1';
    if (file_exists($filename)) {
         echo 'atmam';
         include ('$filename');
    }
    else
         echo 'Failed to download file';

我假设你会在你的站点必须访问的任何文件中使用它,为了节省你的时间,你可以在函数中使用它。

代码语言:javascript
运行
复制
 function testfile(filename)  {
       if (file_exists(filename)) {
         echo 'atmam';
         include ('filename');
      }
      else
         echo 'Failed to download file';
}

如下所示调用函数:

代码语言:javascript
运行
复制
    $filename1 = 'http://downloads.website.com/download/3725f5eea93437e9de52f9b15854f5c1';
    $filename2 = 'something.txt';

    function testfile($filename1);

使用该函数,您可以根据需要检查任意多个文件名,并为每个文件名使用变量。

编辑:要解决即将出现的语法错误,必须确保包含的文件没有错误。请把它贴在这里,让我们看看。删除回显和打印不会改变任何东西,事实上,您需要它们在其中以便进行调试。首先尝试使用我放在这里的一小段代码,这是检查文件是否存在的正确方法,如果存在,则执行一些操作。一旦您使用正确的代码检查文件并将其包含在内,那么您将确保一旦修复了包含文件中的任何问题,您将拥有所需的功能。

希望这能帮到你!-Sean

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8280272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档