PHP 返回部分代码文档

Posted

技术标签:

【中文标题】PHP 返回部分代码文档【英文标题】:PHP returning part of the code document 【发布时间】:2011-01-30 04:29:00 【问题描述】:

我有一个 php 页面,根据在 GET 数据中设置的 action 执行几项不同的操作。视情况而定,它应该返回一些 JSON,但它不做任何应该做的事情,而是返回代码文档本身的下半部分,从行的中间开始。这是它开始的片段:

...
 elseif ($_GET['action'] == 'addtop') 
    if (!isset($_GET['pname']) || !isset($_GET['url']) || !isset($_GET['artist']) || !isset($_GET['album']) || !isset($_GET['file'])) 
        die('Error: Incomplete data!');
    
    if (!file_exists($_GET['pname'].".txt")) 
        die('Error: No such playlist!');
    
    $plist = json_decode(file_get_contents($_GET['pname'].".txt"), true);
    $fh = fopen($_GET['pname'].".txt", 'w') or die('Could not open playlist!');
    array_push($plist, array("artist" => $_GET['artist'], "album" => $_GET['album'], "file" => $_GET['file'], "url" => $_GET['url']));
    fwrite($fh,json_encode($plist));
 elseif ($_GET['action'] == 'delfromp') 
...

当我转到该页面时,这是我得到的:

$_GET['艺术家'], "专辑" => $_GET['专辑'], "文件" => $_GET['file'], "url" => $_GET['url'])); fwrite($fh,json_encode($plist)); elseif ($_GET['action'] == 'delfromp') if (!isset($_GET['pname']) || !isset($_GET['id'])) die('错误: 数据不完整!'); 如果 (!file_exists($_GET['pname'].".txt")) die('错误:没有这样的播放列表!'); $plist = json_decode(file_get_contents($_GET['pname'].".txt"), 真的); $fh = fopen($_GET['pname'].".txt", 'w') 或 die('无法打开播放列表!'); 未设置($plist[$_GET['id']]); $plist = 数组值($plist); fwrite($fh,json_encode($plist)); elseif ($_GET['action'] == 'readp') 如果 (!file_exists($_GET['pname'].".txt")) die('错误:没有这样的播放列表!'); $plist = json_decode(file_get_contents($_GET['pname'].".txt"), 真的); $arr = 数组(“条目” => $plist); $json = json_encode($arr); 回声 $json; elseif ($_GET['action'] == 'getps') $plists = array(); if ($handle = opendir('播放列表')) 而(假!==($播放列表= readdir($handle))) if ($playlist != “。” && $播放列表!=“..”) array_push($plists, substr($playlist, 0, strripos($playlist, '.')-1)); else die('错误:无法打开 播放列表!'); $arr = 数组(“条目”=>$plists); $json = json_encode($arr);回声 $json; 别的 die('错误:没有这样的动作!'); ?>

它从array_push(... 行的中间开始。

我真的想不出它在做什么。它周围的任何地方都没有echos。有什么想法吗?

【问题讨论】:

【参考方案1】:

看起来=> 正在关闭您的 PHP 代码。您是否启用了短标签?还是某种自定义标签?

【讨论】:

啊。那有点奇怪。我用一个短标签开始它,所以我将它更改为常规标签并修复它。虽然我不确定为什么它会开始打印中间的代码......

以上是关于PHP 返回部分代码文档的主要内容,如果未能解决你的问题,请参考以下文章

NEST - 返回部分文档

PHP处理XML文档,没有CDATA部分数据处理

MongoDB通过***属性和嵌套数组键查找文档,并返回匹配文档的一部分

禁用部分页面php的警告和通知

PHP dirname 返回路径中的目录部分

在 mongoose mongodb 中使用 $or 返回文档的一部分