我在CodeIgniter中有一个模型函数,它首先将zip,rar文件上传到服务器位置,然后将其解压缩。
但在此之前,我需要指定文件结构
root folder
--- application
--admin
--controllers
--models
--views
--newsletter
--- system
---
---
---
这是我的模特函数
function add_newsletter()
{
if($_FILES['unextract']['name']=="")
{
return "Successfully added";
}
else if($_FILES['unextract']['name']!=''&&
($_FILES['unextract']['type']=='application/zip'
|| $_FILES['unextract']['type']=='application/x-zip'
|| $_FILES['unextract']['type']=='application/octet-stream'
|| $_FILES['unextract']['type']=='application/x-zip-compressed'
|| $_FILES['unextract']['type']=='application/x-rar-compressed'
|| $_FILES['unextract']['type']=='application/x-rar'
|| $_FILES['unextract']['type']=='application/rar'))
{
if(!file_exists('./application/admin/views/newsletter/'))
mkdir('./application/admin/views/newsletter/');
$file_name=md5(uniqid(rand())).$_FILES['unextract']['name']; // name of the file being changed with encryption, ex cd102453xz_ersnewsletter-1.zip
$image= 'application/admin/views/newsletter/'.$file_name;
$unxt="./".$image;
move_uploaded_file($_FILES['unextract']['tmp_name'],$unxt); //$_FILES['unextract']['tmp_name'] is equal to newsletter-1.zip
$zip = new ZipArchive;
if ($zip->open($unxt) === TRUE)
{
$folder=explode(".",$image);
$zip->extractTo('./'.$folder[0]); // $folder[0] stands for application/admin/views/newsletter/cd102453xz_ersnewsletter-1
$zip->close();
$data['msg']="Uploaded To database";
$title=$file_name;
$timestamp=time();
$data = array(
'newsletter_title' => $title,
'newsletter_timestamp' => $timestamp,
'newsletter_date'=>date('d/m/Y')
);
$msg=$this->insert('td_newsletter',$data,$timestamp);
if($msg=="done")
return "NewsLetter Added To System";
}
else
{
return "NewsLetter Not Added To System";
}
}
}
问题是newsletter-1.zip的文件被提取到位置
application/admin/views/newsletter/cd102453xz_ersnewsletter-1/newsletter-1/
虽然它应该被提取到
application/admin/views/newsletter/cd102453xz_ersnewsletter-1/
现在文件结构是这样的:
--application
--views
--newsletter
--cd102453xz_ersnewsletter-1
--newsletter-1
--screenshot.png
--index.html.php
--images/
虽然我想要的文件结构是
--application
--views
--newsletter
--cd102453xz_ersnewsletter-1
--screenshot.png
--index.html.php
--images/
答案 0 :(得分:0)
当您创建newsletter.zip时,您正在压缩文件夹时事通讯,因此您的zip文件将如下所示
newsletter.zip:
- newsletter :
- file1
- file2
- file3
尝试打开newsletter文件夹,选择文件夹中的文件然后压缩它们,你的结构将是这样的:
newsletter.zip:
- file1
- file2
- file3