-2

Hi I'm trying to create a zip and adding some pdf files into it but getting error when i try to check if the folder exists or not i don't know where I'm doing mistake please tell me if my code is right or wrong . Here is my code .

public function createZips(){

    if (is_dir("/home/servername/public_html/wp-content/pdf_files/".$stdname)){

        if(!(file_exists("/home/servername/public_html/wp-content/pdf_files/".$stdname."/zip/".$ct[-1].".zip"))){
            $zip = new ZipArchive();
            $filename = "/home/servername/public_html/wp-content/pdf_files/".$stdname."/zip/".$ct[-1].".zip";

            if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
                exit("cannot open <$filename>\n");
            }

            $dir = "/home/servername/public_html/wp-content/pdf_files/".$stdname."/".$ct[-1];

            // Create zip
            $this->createZip($zip,$dir);

            $zip->close();
        }
        return ture;
    }
    return false;
}

// Create zip
public function createZip($zip,$dir){
    if (is_dir($dir)){

        if ($dh = opendir($dir)){
            while (($file = readdir($dh)) !== false){

                // If file
                if (is_file($dir.$file)) {
                    if($file != '' && $file != '.' && $file != '..'){

                        $zip->addFile($dir.$file);
                    }
                }else{
                    // If directory
                    if(is_dir($dir.$file) ){

                        if($file != '' && $file != '.' && $file != '..'){

                            // Add empty directory
                            $zip->addEmptyDir($dir.$file);

                            $folder = $dir.$file.'/';

                            // Read data of the folder
                            $this->createZip($zip,$folder);
                        }
                    }

                }

            }
            closedir($dh);
        }
    }
}

Please have a look at this i think this is a simple condition check but i don't know where i'm mistaken My question is not duplicate cause i'm getting an error message that says file is not in the folder here is the error Warning: ZipArchive::close(): Failure to create temporary file: No such file or directory in /home/ameliadeol2015/public_html/wp-content/plugins/lifterlms/includes/admin/reporting/tables/llms.table.student.courses.php on line 137

Huzail Jamil
  • 96
  • 1
  • 1
  • 13
  • Check your error log. I bet there is something about `$ct` not defined – RiggsFolly Jun 04 '18 at 11:11
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Jun 04 '18 at 11:11
  • I'm defining $ct which is taking a string value which i'm using the problem is that i'm getting error that file does not exist while the folder and file is there if (is_dir("/home/servername/public_html/wp-content/pdf_files/".$stdname)) the problem is in this line i'm checking if the folder exist or not . but this is nor working correct how – Huzail Jamil Jun 04 '18 at 11:26
  • I don't see how $ct is available within the scope of your createZips() function. It is not defined within the function, it is not passed to the function and is not a global variable. – Mr Glass Jun 04 '18 at 11:34
  • Warning: ZipArchive::close(): Failure to create temporary file: No such file or directory in /home/ameliadeol2015/public_html/wp-content/plugins/lifterlms/includes/admin/reporting/tables/llms.table.student.courses.php on line 137 – – Huzail Jamil Jun 04 '18 at 11:35
  • @Mr-Glass sir $ct is a class variable which i'm using in – Huzail Jamil Jun 04 '18 at 11:37
  • 1
    @HuzailJamil, if it is a class variable then you should be using $this->ct to access it. – Mr Glass Jun 04 '18 at 11:41

1 Answers1

0

You don't seem to be defining $ct, so when you try to access $ct[-1] it's not going to have a value.

Mr Glass
  • 1,186
  • 1
  • 6
  • 14
  • Warning: ZipArchive::close(): Failure to create temporary file: No such file or directory in /home/ameliadeol2015/public_html/wp-content/plugins/lifterlms/includes/admin/reporting/tables/llms.table.student.courses.php on line 137 – Huzail Jamil Jun 04 '18 at 11:28
  • I'm defining $ct which is taking a string value which i'm using the problem is that i'm getting error that file does not exist while the folder and file is there if (is_dir("/home/servername/public_html/wp-content/pdf_files/".$stdname)) the problem is in this line i'm checking if the folder exist or not . but this is nor working correct how – Huzail Jamil Jun 04 '18 at 11:29
  • Who voted this down? It is the correct answer. $ct is not defined. It turns out he should be using $this->ct. – Mr Glass Jun 04 '18 at 13:01
  • i don't know who turned this down – Huzail Jamil Jun 04 '18 at 15:09