Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP中文件_get_contents()的奇怪行为_Php_Cakephp_File Get Contents - Fatal编程技术网

CakePHP中文件_get_contents()的奇怪行为

CakePHP中文件_get_contents()的奇怪行为,php,cakephp,file-get-contents,Php,Cakephp,File Get Contents,在过去,我使用file_get_contents()通过CakePHP下载文件,效果很好 然而,我正在一个新网站上工作,当我使用file_get_contents()下载csv或zip文件(没有尝试其他文件)时,我会在客户端计算机上创建的文件的开头标记一些奇怪的东西 使用记事本++看起来好像: LF tabspacelf tabspacespace 正在添加。下一行文本似乎是缩进的(但这可能正是notepad++解释上述字符的方式——或者notepad++可能没有显示另一个字符!) 我已经看了又

在过去,我使用file_get_contents()通过CakePHP下载文件,效果很好

然而,我正在一个新网站上工作,当我使用file_get_contents()下载csv或zip文件(没有尝试其他文件)时,我会在客户端计算机上创建的文件的开头标记一些奇怪的东西

使用记事本++看起来好像:

LF
tabspacelf
tabspacespace

正在添加。下一行文本似乎是缩进的(但这可能正是notepad++解释上述字符的方式——或者notepad++可能没有显示另一个字符!)

我已经看了又看我的控制器和视图,看看是否有东西被添加了,但我真的看不到任何不好的地方

以前有人见过这样的行为吗?如果有,你发现是什么原因造成的吗

(我完全理解这可能是我编写的不好的代码,但我希望这是一个简短的问题,而不是包含大量的代码..)

非常感谢

保罗

以下是我认为相关的代码片段:

在config/routes.php中

Router::parseExtensions('csv');
Router::parseExtensions('pdf');
Router::parseExtensions('zip');
在AppController.php中

class AppController extends Controller {
//...

    public $components = array(
        'Session',
        'Acl',
        'RequestHandler',
        'Paginator'
    );
在相关型号的控制器中:

public function add_download($pass){
//create a record in the database table VisitDownloads
//recording who is downloading what and when

$this->loadModel('JournalArticleDataset');
$this->VisitDownload->create();
$this->VisitDownload->set(array('visit_id' => $this->Session->read('Visit.id'),
'journal_article_dataset_id' => $this->request->params['pass'][0],
'download_time' => date('Y-m-d H:i:s')));
//record is saved to database
$this->VisitDownload->save();
//build filename string to identify fully the resource being downloaded,
// then download
$filename = $this->JournalArticleDataset->get_filename($this->request->params['pass'][0]);
$filestring = "/files/".$filename;
if(substr($filename,-3)=='csv'){
header('Content-Type: text/plain');
header("Content-Disposition: attachment; filename=\"". substr($filename,0,strlen($filename)-4) ."_".date('Y-m-d_H:i:s').".".substr($filename,-3)."\"");
}
if(substr($filename,-3)=='zip'){
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=\"". substr($filename,0,strlen($filename)-4) ."_".date('Y-m-d_H:i:s').".".substr($filename,-3)."\"");
}


$filecontent = file_get_contents($filestring);
echo $filecontent;
exit();
}
然后在视图中显示此操作的链接:

echo "<table class='rightmove'>";
foreach($journalArticle['JournalArticleDataset'] as $Datasets) :
$filestring = "/srv/www/actc.lshtm.ac.uk/public/openaccess/webroot/files/" . $Datasets['dataset_resource_name'];
$extension = pathinfo($Datasets['dataset_resource_name'], PATHINFO_EXTENSION);

?>
 <tr>
            <td><?php 

            $filesize = human_filesize(filesize($filestring), $decimals = 2);
            echo $this->Html->image("/fileicons/".$extension .".png", array('url'=>array('controller'=>'VisitDownloads', 'action'=>'add_download',$Datasets['id'])));
            echo "\r\n<small>(". $filesize .")</small>" ;
            ?></td>
            <td><?php echo $Datasets['dataset_resource_description']; ?></td>
            <td><?php echo $Datasets['dataset_resource_name']; ?></td>
            <td><?php echo $Datasets['licence_type']; ?></td>
</tr>
<?php
endforeach;
echo "</table>";
echo”“;
foreach($journalArticle['JournalArticleDataset']作为$DataSet):
$filestring=“/srv/www/actc.lshtm.ac.uk/public/openaccess/webroot/files/”$数据集['dataset_resource_name'];
$extension=pathinfo($Datasets['dataset\u resource\u name',pathinfo\u extension);
?>

你能发布导致问题的代码吗?好的,我已经把相关的(我想!)代码放上去了。我希望这有帮助。