Can';t创建zip文件(php)
所以我在互联网上发现了这个类,它可以将文件放入zip。 我有一个表格,我可以上传图片到一个文件夹,然后我试图循环到这些文件,并将它们存储到一个zip文件,但似乎我失败了 拉链类 最后一段代码在foreach中,该foreach循环到上传的文件中 我不知道为什么我最终未能创建存档Can';t创建zip文件(php),php,zip,archive,Php,Zip,Archive,所以我在互联网上发现了这个类,它可以将文件放入zip。 我有一个表格,我可以上传图片到一个文件夹,然后我试图循环到这些文件,并将它们存储到一个zip文件,但似乎我失败了 拉链类 最后一段代码在foreach中,该foreach循环到上传的文件中 我不知道为什么我最终未能创建存档 将这些新创建的文件夹转换为zip或添加到zip中可能是一种简单的方法?第一个问题是因为我添加了基本URL,我必须编写。/upload/。 第二个问题有点愚蠢。。。 我必须在foreach$zippers=newzippe
将这些新创建的文件夹转换为zip或添加到zip中可能是一种简单的方法?第一个问题是因为我添加了
基本URL
,我必须编写。/upload/
。
第二个问题有点愚蠢。。。
我必须在foreach
$zippers=newzippers之外创建类
和$zipper-存储(“../upload/”$uniqUser..zip”)代码>用于不必要的重复(添加在底部)。可能重复的I可能是错误的,我无法测试atm,但如果我没有弄错,您需要跳过一些限制,以便文件_exists()使用完整url。尝试只传递文件的相对位置,而不传递基本URL。如果(!file_存在($file)){unset($this->_files[$index]),print_r($unset.$file);}和foreach($this->_filesas$file){$this->_zip->addFile($file,$file)}{print_r($Added file:$file)}
@ThomasB我从print_r($file($file)中什么也得不到
表示没有删除任何文件,并且从打印中(“添加的文件:.$file”)代码>我得到已经添加的3个文件。我确实更改了BASE_URL,它工作了,但问题是,在打开存档后,我只发现上传的最后一个文件,即使$file表示添加了所有文件,但实际上只添加了最后一个文件。添加的文件:../upload/path/7 001.jpg添加的文件:../upload/path/8 001.jpg添加的文件:../upload/path/9 001.jpg@ThomasB
<?php
class zipper {
private $_files = array(),
$_zip;
public function __construct() {
$this->_zip = new ZipArchive;
}
public function add($input){
if(is_array($input)){
$this->_files = array_merge($this->_files, $input);
}else{
$this->_files[] = $input;
}
}
public function store($location = null){
if(count($this->_files) && $location){
foreach ($this->_files as $index => $file) {
if(!file_exists($file)){
unset($this->_files[$index]);
}
print_r($file . "<br>");// here gives me the exact path of the files.
}
if($this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
foreach ($this->_files as $file) {
$this->_zip->addFile($file, $file);
}
$this->_zip->close();
}
}
}
}
> $uniqUser = uniqid(strtoupper($userName). "-" , true);
> $directory ='../upload/';
> $file_name is $files['name']
if(!is_dir("../upload/". $uniqUser ."/")) {
mkdir("../upload/". $uniqUser ."/");
}
if(move_uploaded_file($file_tmp, $directory . $uniqUser . "/" . $file_name)){
$uploaded[$position] = $directory . $uniqUser . "/" . $file_name;
$zipper = new zipper;
$zipper->add(BASE_URL . "/upload/" . $uniqUser . "/" . $file_name);
$zipper->store(BASE_URL . "/upload/" . $uniqUser . ".zip");