我对这个temp目录一无所知,并且使用PHP在后端管理文件,所以任何帮助都将非常有用。因此,这里的要点是,我单击下载一个特定文件或所有文件。诀窍在于,根据特定条件,数据将从API作为流返回,而另一个作为内容(在本例中为html)返回。在我看来,这种逻辑应该起作用。我收到的消息返回为true或false,以确定它是流还是文件的内容。但是关于这两个条件的事情正在打破。我设法使各种情况奏效。个别而言,它们下载得很好,但是如果我移动代码,则大量下载会中断或以其他方式中断。有什么建议吗?
public function downloadTranslations(Request $request, $id)
{
$target_locales = $request->input("target_locale");
$has_source = $request->input("source");
$client = new API(Auth::user()->access_token, ZKEnvHelper::env('API_URL', 'https://myaccount.site.com'));
$document = Document::find($id);
$job_document = JobDocument::where('document_id', $id)->first();
$job = Job::find($job_document->job_id);
$file = tempnam('tmp', 'zip');
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
$name_and_extension = explode('.', $document->name);
if($target_locales == null){
$target_locales = [];
foreach ($job->target_languages as $target_language) {
$target_locales[] = $target_language['locale'];
}
}
foreach($target_locales as $target_locale){
$translation = $client->downloadDocument($document->document_id, $target_locale);
$stream = Stream::factory($translation->get('body'));
$filename = $name_and_extension[0] . ' (' . $target_locale . ').' . $name_and_extension[1];
if($translation->get('message') == 'true') {
$tmpfile = tempnam(sys_get_temp_dir(), 'lingo');
$handle = fopen($tmpfile, 'w');
fwrite($handle, $stream->getContents());
$zip->addFile($tmpfile, $filename);
fclose($handle);
}
else if($translation->get('message') == 'false'){
$zip->addFromString($filename, $translation->get('body'));
};
}
$translation = $client->downloadDocument($document->document_id, null, null);
$filename = $name_and_extension[0]. ' (Source).'.$name_and_extension[1];
$zip->addFromString($filename, $translation->get('body'));
$zip->close();
return response()->download($file, $name_and_extension[0].'.zip')->deleteFileAfterSend(true);
答案 0 :(得分:0)
我在您的代码中发现了这个,我什至不知道它不会刹车:
else if($translation->get('message') == 'false'){
$zip->addFromString($filename, $translation->get('body'));
};
我不认为分号应该存在...
我还在您的代码上看到以下内容:$file = tempnam('tmp', 'zip');
请检查此函数是否确实在创建不同的值,方法是返回此字符串,否则,流中的请求可能存在某些竞争条件。
这是我几周前遇到的问题。尝试这样的事情:根据您的需要添加我的代码。
$zip_name = 'yourAmazing.zip';
$zip_path = storage_path('temp/');
// If the zip path doesnt exist, create it.
if (!file_exists($zip_path)) {
File::makeDirectory($zip_path);
mkdir($zip_path, 666, true);
}else{
// Do nothing realy
}
// If the zip file already exist, delete it so you don't have problems when trying to create it again and you don't have to validate if each file is on there
if (file_exists($zip_path . $zip_name)) {
File::delete($zip_path . $zip_name);
}
// create your zip
$zip = Zip::create($zip_path . $zip_name);
// add your temp folder files
$zip->add(storage_path('/temp/folderofyourfiles'));
$zip->close();
// DELETE directory where the files where stored,
// since they are already on the zip
File::deleteDirectory(storage_path('/temp/folderofyourfiles'));
// DELETE AFTER SEND does what it says assuring it delete the temp file
// However, if the transfer is interrupted, the file wont delete, since the
// send event was not terminated. Leaving the file there, thats why i check
// that the ZIP doesn't exist already
return response()
->download($zip_path . $zip_name, 'The name for your download')
->deleteFileAfterSend(true);