Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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
Can';用php脚本访问文件_Php_File - Fatal编程技术网

Can';用php脚本访问文件

Can';用php脚本访问文件,php,file,Php,File,今天我想创建一些php脚本,它将强制直接下载mp3文件 我想下载的文件放在sounds文件夹中,我可以通过 但当谈到php脚本时,函数文件_exists表示,该文件不在该服务器上 我怎样才能修好它 我用来下载文件的代码 if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header

今天我想创建一些php脚本,它将强制直接下载mp3文件

我想下载的文件放在sounds文件夹中,我可以通过

但当谈到php脚本时,函数文件_exists表示,该文件不在该服务器上

我怎样才能修好它

我用来下载文件的代码

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file_name));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
以及获取文件名和文件url

$file_name = ''.$sound['Sounds']['artist'].' - '.$sound['Sounds']['title'].'.mp3';

$file_name = preg_replace('/\s+/', '_', $file_name);

$file = 'http://playall.pl'.$this->webroot.'sounds/'.$sound['Sounds']['src_url'];
对于
file\u存在($file)
变量
$file
需要完整路径。并且必须是文件系统上的路径,而不是web位置的url

所以你需要一些类似的东西:

$file = $_SERVER['DOCUMENT_ROOT'] . '/app/webroot/sounds/';

$\u服务器['DOCUMENT\u ROOT']
为您提供了htdocs文件夹的文件系统路径,因此其余文件需要与此相对。

确定。问题很简单-我在path前面有一个/。现在它工作正常了。谢谢大家的帮助

为什么不直接重定向浏览器?因为我想在点击按钮时强制下载此文件。要添加,我使用CakePHP框架。
file_存在
检查文件系统,而不是Internet所以,当我使用CakePHP时,文件在app/webroot/sounds/文件夹中放置的文件song.mp3的url是什么样子?