使用php进行文件备份

Vis*_*ish 2 php backup

我想使用php创建给定文件夹中所有文件的快照,然后压缩它.

我怎样才能做到这一点.正在将内置函数压缩到php.也有压缩的替代方案.

您为代码准备了什么样的文件备份系统.我这样做是为了一个开源应用程序,所以它不是备份我的特定系统,所以它必须纯粹用PHP,因为人们不会总是知道如何安装某些应用程序.

多谢你们.

Law*_*one 6

已经回答了 - PHP递归备份脚本

编辑

添加一个旧的,极差的原始答案......

这是一个基本上使用的简单类,

用法:您只需将项目路径作为构造参数传递.它将递归压缩并将项目存储在一个名为的文件夹中./project_backups/,您可以选择设置第二个构造参数,以便仅将文件作为下载文件发送.与其他答案略有不同的东西.

<?php
//Example Usage/s
$backup = new BackupMyProject('./path/to/project/yada');

print_r($backup);
/*
Then your have the object properties to determine the backup

$backup = BackupMyProject Object
(
    [project_path] => ./path/to/project/yada
    [backup_file] => ./project_backups/yada.zip
)

Alternatively set the second parameter and just send the project as a download.
BackupMyProject('./path/to/project/yada', true);
*/


/**
 * Zip a directory into a backups folder, 
 *  optional send the zip as a download
 * 
 * @author Lawrence Cherone
 * @version 0.1
 */
class BackupMyProject{
    // project files working directory - automatically created
    const PWD = "./project_backups/";

    /**
     * Class construct.
     *
     * @param string $path
     * @param bool $download
     */
    function __construct($path=null, $download=false){
        // check construct argument
        if(!$path) die(__CLASS__.' Error: Missing construct param: $path');
        if(!file_exists($path)) die(__CLASS__.' Error: Path not found: '.htmlentities($path));
        if(!is_readable($path)) die(__CLASS__.' Error: Path not readable: '.htmlentities($path));

        // set working vars
        $this->project_path = rtrim($path, '/');
        $this->backup_file  = self::PWD.basename($this->project_path).'.zip';

        // make project backup folder
        if(!file_exists(self::PWD)){
            mkdir(self::PWD, 0775, true);
        }

        // zip project files
        try{
            $this->zipcreate($this->project_path, $this->backup_file);
        }catch(Exception $e){
            die($e->getMessage());
        }

        if($download !== false){
            // send zip to user
            header('Content-Description: File Transfer');
            header('Content-Type: application/zip');
            header('Content-Disposition: attachment; filename="'.basename($this->backup_file).'"');
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: '.sprintf("%u", filesize($this->backup_file)));
            readfile($this->backup_file);
            // cleanup
            unlink($this->backup_file);
        }
    }

    /**
     * Create zip from extracted/fixed project.
     *
     * @uses ZipArchive
     * @uses RecursiveIteratorIterator
     * @param string $source
     * @param string $destination
     * @return bool
     */
    function zipcreate($source, $destination) {
        if (!extension_loaded('zip') || !file_exists($source)) {
            throw new Exception(__CLASS__.' Fatal error: ZipArchive required to use BackupMyProject class');
        }
        $zip = new ZipArchive();
        if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
            throw new Exception(__CLASS__. ' Error: ZipArchive::open() failed to open path');
        }
        $source = str_replace('\\', '/', realpath($source));
        if (is_dir($source) === true) {
            $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
            foreach ($files as $file) {
                $file = str_replace('\\', '/', 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));
                }
            }
        }
        return $zip->close();
    }

}
Run Code Online (Sandbox Code Playgroud)