CodeIgniter Forums
File in use? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: File in use? (/thread-13407.html)



File in use? - El Forum - 11-21-2008

[eluser]Lazos[/eluser]
I have this code to upload a zip file and then immediately to unzip it. If the operation fail because in the directory does not have any css files or the template file does not exist I call the function deleteDir to delete what was extracted from the zip file. Normally in my Web FileManager application is deleting all Files and Folders the deleteDir function but here because I am calling the get_filenames function PHP returns that the directory is not empty. When I do not call that function it deletes everything. It seems like the directory is in use by the get_filenames so it cannot remove it Sad.

I think the same thing is happening also when I try to unlink the zip file. I get the error permission denied.
If I use my Web Filemanager to delete the zip everything is working normally.

Any ideas how to solve this?

Code:
$data = $this->upload->data();
            $config['fileName']  = $data['file_path'].$data['file_name'];
            $config['targetDir'] = $data['file_path'];
            $this->unzip->initialize($config);
            $this->unzip->unzipAll();
            $css = get_filenames($data['file_path'].$data['raw_name'].'/css/');
            if (!file_exists($data['file_path'].$data['raw_name'].'/template.tpl') || (empty($css))) {

                $this->filemanipulation->deleteDir($data['file_path'].$data['raw_name']);
unlink($data['file_path'].$data['file_name']);
            }


Code:
function deleteDir($dir) {
        $dir_content = scandir ($dir);
        if ($dir_content !== FALSE) {
            foreach ($dir_content as $entry)
            {
                if (!in_array($entry, array ('.','..'))){
                    $entry = $dir. '/'. $entry;
                    if (!is_dir($entry)) {
                        unlink($entry);
                    }
                    else {
                    $this->deleteDir($entry);
                    }
                }
            }
            rmdir ($dir);
            return true;
        }
    }



File in use? - El Forum - 11-21-2008

[eluser]Lazos[/eluser]
To CodeIgniter people.

I do not know if is correct but please tell me asap. In the get_filenames function you use the opendir() but at the end there is no closedir().

I changed your helper and I included the closedir() before returning the data. This was the problem?

EDIT1: The same goes for unziping the file. After you call the $this->unzip->unzipAll(); just use also the $this->unzip->close(); to close the file handle.

Code:
if ( ! function_exists('get_filenames'))
{
    function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
    {
        static $_filedata = array();
                
        if ($fp = @opendir($source_dir))
        {
            // reset the array and make sure $source_dir has a trailing slash on the initial call
            if ($_recursion === FALSE)
            {
                $_filedata = array();
                $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
            }
            
            while (FALSE !== ($file = readdir($fp)))
            {
                if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
                {
                     get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
                }
                elseif (strncmp($file, '.', 1) !== 0)
                {
            
                    $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
                }
            }
            @closedir($fp);
            return $_filedata;
        }
        else
        {
            @closedir($fp);
            return FALSE;
        }
    }
}