使用PHP自动下载文件

使用PHP自动下载文件,php,Php,我正在编写一个小脚本,用php将csv转换为json。 我想,当你上传文件和转换完成后,转换后的文件会自动下载 exec(/*some stuff*/); // i call the script that convert the csv into json, i get my converted file path back chmod($newFilePath, 0777); // i give all access to the new file startDownload($newFil

我正在编写一个小脚本,用php将csv转换为json。 我想,当你上传文件和转换完成后,转换后的文件会自动下载

exec(/*some stuff*/); // i call the script that convert the csv into json, i get my converted file path back
chmod($newFilePath, 0777); // i give all access to the new file
startDownload($newFilePath); // i ask the autoDownload
这里是下载功能

function startDownload($path){
    if (file_exists($path)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($path));
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($path));
        readfile($path);
        exit();
    }
}
如果我回显readfile$path;或者filesize$path我得到了我想要的文件内容和好的信息,我确信路径是正确的。但是当我执行脚本时,什么也没有发生

我可能错过了一些愚蠢的事情^^

如何让用户下载此json文件

我试图建立一个链接,他也这么做了,这是访问问题还是类似的问题

编辑:

我在本地测试了这个脚本,它工作得很好,所以问题是权限之类的。我对这类东西很在行,所以如果你有什么想法可以帮忙的话^^

谢谢你的帮助

执行后缺少分号:

我会检查chmod是否真的更改了路径权限:

if(chmod($newFilePath, 0777)){
    startDownload($newFilePath); 
}else{
    die('Permission denied');
}

在打开服务器后立即将错误报告添加到文件顶部隐藏错误,因此我在本地进行了测试,效果非常好。所以这可能是权限或类似的问题,但我不喜欢这些东西。你有什么想法吗^^如果包含上面我共享的代码,服务器不应该隐藏错误,除非有其他内容覆盖了错误显示。如果它在本地工作得很好,您就不会像下面指出的那样缺少分号。@JayBlanchard$path是转换后创建的json文件的路径。另外,我认为服务器覆盖了php错误,它是一个prod/dev服务器,因此这种错误消息可能被隐藏:sTry要运行readfile$newFilePath;我在stackOverflow上重写了这部分代码以使其更清晰,而不是在第一部分下载以检查文件是否被读取Ok是sry,但实际脚本中有所有分号^^^可能需要检查chmod权限,或者可能需要检查exec内容?好的,它已修复,文件夹中存在问题,没有良好的权限。谢谢你的帮助,很抱歉浪费了你的时间^^
if(chmod($newFilePath, 0777)){
    startDownload($newFilePath); 
}else{
    die('Permission denied');
}