Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重写一段php代码,以便将文件复制到文件夹_Php_Zip - Fatal编程技术网

重写一段php代码,以便将文件复制到文件夹

重写一段php代码,以便将文件复制到文件夹,php,zip,Php,Zip,这是简单的php代码,但我是一名asp开发人员 任何人都可以重写这段代码,所以它不必将文件压缩成zip格式,而只是将它们复制到“wwwroot”文件夹中 <?php /* CONFIG */ $pathToAssets = array("elements/bootstrap", "elements/css", "elements/fonts", "elements/images", "elements/js"); $filename = "tmp/website.zip"; //us

这是简单的php代码,但我是一名asp开发人员

任何人都可以重写这段代码,所以它不必将文件压缩成zip格式,而只是将它们复制到“wwwroot”文件夹中

<?php

/* CONFIG */

$pathToAssets = array("elements/bootstrap", "elements/css", "elements/fonts", "elements/images", "elements/js");

$filename = "tmp/website.zip"; //use the /tmp folder to circumvent any permission issues on the root folder

/* END CONFIG */


$zip = new ZipArchive();

$zip->open($filename, ZipArchive::CREATE);


//add folder structure

foreach( $pathToAssets as $thePath ) {

    // Create recursive directory iterator
    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator( $thePath ),
        RecursiveIteratorIterator::LEAVES_ONLY
    );

    foreach ($files as $name => $file) {

        if( $file->getFilename() != '.' && $file->getFilename() != '..' ) {

            // Get real path for current file
            $filePath = $file->getRealPath();

            $temp = explode("/", $name);

            array_shift( $temp );

            $newName = implode("/", $temp);

            // Add current file to archive
            $zip->addFile($filePath, $newName);

        }

    }

}



foreach( $_POST['pages'] as $page=>$content ) {

    $zip->addFromString($page.".html", $_POST['doctype']."\n".stripslashes($content));

    //echo $content;

}

//$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
//$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");

$zip->close();


$yourfile = $filename;

$file_name = basename($yourfile);

header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Length: " . filesize($yourfile));

readfile($yourfile);

unlink('website.zip');

exit;?>

此外,如果可以,请将文件解压缩到目标文件夹,并在完成后删除原始zip文件。

请参阅

替换此项:

        // Add current file to archive
        $zip->addFile($filePath, $newName);
为此:

        copy($filePath, $_SERVER["DOCUMENT_ROOT"] . '/' . $name);

请注意,SO不是免费的编码服务。我们不是来为你工作的。我们在这里帮助您解决在自己的代码中遇到的特定问题。所以,从你自己开始,如果你遇到了严重的问题,那么是时候来这里,发布你的代码并指出你遇到的问题。如果你真的想找人为你工作,然后我建议你聘请一个付费程序员.tanx作为回应,但我不喜欢php,我也不想在我的工作中使用它。一些朋友请帮我一个忙,所以我发布了这个问题。尊重你可以忽略它。请阅读关于这个网站的工作原理以及为什么这些规则已经达成一致。看来你不知道.tanx在这方面帮了我的忙,但它不起作用。我也读过copy,但我注意到调试php的事实让我很恼火