adding remote files to a zip file

回眸只為那壹抹淺笑 提交于 2021-01-27 18:30:49

问题


Is there a way to add files to a zip file from another server with php's zip extension? ie.

addFile(array('localfile.txt,'http://www.domain.com/remotefile.txt'))
//(that obviously does not work)

I suppose I can download the files to a temporal folder and then add them to the zip file, but I was looking for a more automated solution or a function already made


回答1:


use file_get_contents() and ZipArchive::addFromString()

$zipArchiveInstance->addFromString($filename, file_get_contents($mediaUrl));

This writes the contents fetched remotely straight into your php object (no need to write/read temp file)




回答2:


It's not hard to read remote files in PHP.

file_get_contents("http://example.com/remote.txt");

Or to copy them locally:

copy("http://example.com/remote.txt", "/tmp/local.txt");

Whichever way you do it, the contents are going to have to be transferred to a local temp folder or memory before you can do anything with them.




回答3:


Fetch them with cURL, add them from TEMP directory.



来源:https://stackoverflow.com/questions/2956177/adding-remote-files-to-a-zip-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!