对于我使用guzzle下载的gzip文件,Unlink不起作用


Unlink doesn't work for gzipped files that I downloaded using guzzle

我用foreach循环下载了很多gzip文件,在每个循环中解压缩文件。到目前为止,一切都很好,但是在循环结束时,我想解链接gzip文件。代码如下:

...
... // Previous processes
...
$destinationPath = './files/lld/' . $data['hour'] . '.gz';
fopen($destinationPath, 'w+');
...
... // Download processes
...
// Unzip
$gzfile = gzopen($destinationPath, "rb");
$tsvFile = fopen($destinationTsvPath, "w");
while ( ! gzeof($gzfile)) 
{
    $string = gzread($gzfile, 4096);
    fwrite($tsvFile, $string, strlen($string));
}
gzclose($gzfile);
fclose($tsvFile);
// Delete
unlink($destinationPath);

除unlink进程外,一切正常,没有任何错误日志。我在这个网站上读过类似的问题,其中一个答案是在gzlose()函数之后使用unlink()。我试过了,但是没有结果。

似乎您没有足够的权限来删除该文件。我建议您在unlink()之前执行chmod,像这样(这将尝试更改权限):

fclose($destinationPath);
chmod($destinationPath, 777);
unlink($destinationPath);

如果这不起作用,那么设置目录,您存储这些文件的地方,775的权限。您可以通过SSH或FTP来实现。