php - Save zip file in a different directory than the level at which code file is present -
i have written code zip content of given directory, code runs want save zip file in different directory calling function.
code:
$source = realpath('application'); $address = realpath('backup'); $destination = $address . directory_separator . 'download.zip'; if (file_exists($destination)) { echo "the file $filename exists"; } else { echo "the file $filename not exist"; } zip($source, $destination); function zip($source, $destination) { if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ziparchive(); if (!$zip->open($destination, ziparchive::create)) { return false; } $source = str_replace('\\', '/', realpath($source)); if (is_dir($source) === true) { $files = new recursiveiteratoriterator(new recursivedirectoryiterator($source), recursiveiteratoriterator::self_first); foreach ($files $file) { $file = str_replace('\\', '/', $file); // ignore "." , ".." folders if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ) continue; $file = realpath($file); if (is_dir($file) === true) { $zip->addemptydir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file) === true) { $zip->addfromstring(str_replace($source . '/', '', $file), file_get_contents($file)); } } } else if (is_file($source) === true) { $zip->addfromstring(basename($source), file_get_contents($source)); } return $zip->close(); }
the zip file not created code, if this
$destination = 'zip.php';
the zip file created. do save file in other directory.
are sure, backup dir exists? create zip file inside backup folder. append bellow code existing code
$address = realpath('backup'); if (!file_exists($address)){ if (!mkdir($address, 0777, true)) { die('failed create folder.'); } }
hope solve problem. or else check backup folder file access permissions.
Comments
Post a Comment