这是我在控制器内部的get_status函数。我正在尝试在给定路径'/var/www/html/hdfcdsademo/uploadpdf/'.$this->session->userdata("product_type"(中生成zip fle。/'.
public function get_status(){
$query=$this->db->query("select (select COUNT(DISTINCT(dsahubwise)) FROM month_3_lap where isgenerated=0 && product_pre='".$this->session->userdata("product_type")."') as pending,(select COUNT(DISTINCT(dsahubwise)) FROM month_3_lap where isgenerated=1 && product_pre='".$this->session->userdata("product_type")."') as generate");
$result=$query->row();
if($result->pending==0){
$this->generatepdf_model->createZipFromDir('/var/www/html/hdfcdsademo/uploadpdf/'.$this->session->userdata("product_type").'/','/var/www/html/hdfcdsademo/uploadpdf/'.$this->session->userdata("product_type").'/LAP_DSA.zip');
$this->db->query("UPDATE generatepdf SET isgenerate=0,is_archive=1 where product_pre='".$this->session->userdata("product_type")."'");
}
echo json_encode(['pending'=>$result->pending,'generate'=>$result->generate]);
}
这是我在模型中的createZipFromDir函数。
public function createZipFromDir($dir, $zip_file) {
$zip = new ZipArchive;
if (true !== $zip->open($zip_file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
return false;
}
$this->zipDir($dir, $zip);
return $zip;
}
public function zipDir($dir, $zip, $relative_path = DIRECTORY_SEPARATOR) {
$dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file === '.' || $file === '..') {
continue;
}
if (is_file($dir . $file)) {
$zip->addFile($dir . $file, $file);
} elseif (is_dir($dir . $file)) {
$this->zipDir($dir . $file, $zip, $relative_path . $file);
}
}
}
closedir($handle);
}
在将文件上传到目录之前,您必须在服务器上创建目录:
public function get_status(){
$query=$this->db->query("select (select COUNT(DISTINCT(dsahubwise)) FROM month_3_lap where isgenerated=0 && product_pre='".$this->session->userdata("product_type")."') as pending,(select COUNT(DISTINCT(dsahubwise)) FROM month_3_lap where isgenerated=1 && product_pre='".$this->session->userdata("product_type")."') as generate");
$result=$query->row();
if($result->pending==0){
$dir='/var/www/html/hdfcdsademo/uploadpdf/'.$this->session->userdata("product_type");
if (!file_exists($dir)) {
mkdir($baseDir, 0777, true);
}
$this->generatepdf_model->createZipFromDir('/var/www/html/hdfcdsademo/uploadpdf/'.$this->session->userdata("product_type").'/','/var/www/html/hdfcdsademo/uploadpdf/'.$this->session->userdata("product_type").'/LAP_DSA.zip');
$this->db->query("UPDATE generatepdf SET isgenerate=0,is_archive=1 where product_pre='".$this->session->userdata("product_type")."'");
}
echo json_encode(['pending'=>$result->pending,'generate'=>$result->generate]);
}