When using system to zip a directory lots of output is shown
I'm trying to write a script that allows an admin of a photo uploading system download all their photos at once. Currently I am using
system('zip -r '.$_SERVER['DOCUMEN开发者_开发百科T_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads';
to zip the files but this seems to echo names and locations all the files onto the page. Is there anyway to get around this? If not what is the best way to zip files on server.
You might use exec('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads');
instead.
You can use ZipArchive extension instead (if you are allowed to) of calling system zip like that, because it makes your code non-portable.
You can also use output buffering:
ob_start();
system('zip -r '.$_SERVER['DOCUMENT_ROOT'].'/zip.zip '.$_SERVER['DOCUMENT_ROOT'].'/images/photo-uploads';
ob_end_clean();
This will stop any output from being shown from the system command.
精彩评论