简体   繁体   中英

How to zip files inside a PHP webjob on Azure?

I'm in the process of moving a PHP site and associated scheduled tasks over to Azure.

I can get most of the webjobs to run, but one in particular falls over when it tries to execute a command-line 'zip' command (presumably because Azure doesn't have this utility available):

exec('zip -9 -j '.$tmpdir.'GROUP-'.date('dmY').'-DMS14.zip '.$zip_images);

Gives: 'zip' is not recognized as an internal or external command

Is there an equivalent module/command that can be run on Azure to compress files?

As Azure Web sites are running on Windows VM hosted on IIS, and “zip” is not Windows build-in command, so we can't execute it in PHP.

@Rick Rainey's idea of leveraging PHP build-in ZipArchive class to acquire this was right, we don't need to install it on Azure, and I'd like to add some tips:

1, When we add a Webjob, the job script file will be upload at D:\\home\\site\\wwwroot\\App_Data\\jobs and be separated into continuous and triggered as subfolder.

2, As IIS uses FastCGI to handle these PHP scripts, $_SERVER variables will not be defined here. So we can use absolute directory of files instead.

Here is my test code snippet:

$zip = new ZipArchive();
$filename = "D:/home/site/wwwroot/test112.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFile("D:/home/site/wwwroot/.user.ini");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();

You need to do this programmatically rather than trying to launch another process.

It appears that PHP has libraries/functions built-in to support what you want to do. A quick search revealed this documentation explaining how.

If you want a .NET solution then you can use the ZipFile class .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM