[英]Rewrite a piece of php code for copy file to folder
这是简单的 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.