Welcome Guest, Not a member yet? Register   Sign In
Missing .htaccess in zip file
#1

[eluser]spec[/eluser]
I use zip functionality in CI for making quick backups during my proj. dev., but .htacess files are not included in the resulting zip file. ??
#2

[eluser]InsiteFX[/eluser]
Thats because they are hidden files and protected by the server.

InsiteFX
#3

[eluser]spec[/eluser]
May I archive them somehow?
#4

[eluser]bubbafoley[/eluser]
What does your code look like?

this should work:
Code:
$this->zip->read_file('/path/to/.htaccess');
#5

[eluser]spec[/eluser]
Here's the code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;

&lt;head&gt;
    &lt;meta http-equiv="Refresh" content="2; URL=http://localhost/proj" /&gt;
    &lt;meta name="author" content="" /&gt;

    &lt;title&gt;Backup&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;?php class Backup extends Controller {

    function __construct() {
        parent::Controller();
    }

    function index() {
        
        //DB backup:
        $this->load->dbutil();

        $backup = &$this->dbutil->backup();

        $this->load->helper('file');
        $date = strftime("%d-%b-%y at %Hh%M ");
        $db_backup_file = $date . 'db.gz';

        write_file('d:/htdocs/backup/proj/' . $db_backup_file, $backup);
        //<-DB backup
        
        //proj backup:
        $this->load->lib('zip');
        $path = 'd:/htdocs/proj/';

        $this->zip->read_dir($path);
        $date = strftime("%d-%b-%y at %Hh%M ");
        $proj_backup_file = $date . 'proj.zip';
        $this->zip->archive('d:/htdocs/backup/proj/' . $proj_backup_file); // Creates a file named myarchive.zip
        $data = 'Done. Redirecting in 2 sec<br>
        <strong>' . $db_backup_file . '</strong> - '.round((filesize('d:/htdocs/backup/proj/' . $db_backup_file)/1024),1).' KB <br>
        <strong>' . $proj_backup_file . '</strong> - '.round((filesize('d:/htdocs/backup/proj/'.$proj_backup_file)/1024),1).' KB <br>';
        
        $this->out->set_out($data);//display status info
        //<-proj backup
    }

} ?&gt;

&lt;/body&gt;
&lt;/html&gt;
#6

[eluser]bubbafoley[/eluser]
read_dir() ignores hidden files. You'll need to use read_file() to add the .htaccess file

system/libraries/Zip.php
Code:
function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
    {
        if ( ! $fp = @opendir($path))
        {
            return FALSE;
        }

        // Set the original directory root for child dir's to use as relative
        if ($root_path === NULL)
        {
            $root_path = dirname($path).'/';
        }

        while (FALSE !== ($file = readdir($fp)))
        {
            // ignoring hidden files
            if (substr($file, 0, 1) == '.')
            {
                continue;
            }

            if (@is_dir($path.$file))
            {
                $this->read_dir($path.$file."/", $preserve_filepath, $root_path);
            }
            else
            {
                if (FALSE !== ($data = file_get_contents($path.$file)))
                {
                    $name = str_replace("\\", "/", $path);

                    if ($preserve_filepath === FALSE)
                    {
                        $name = str_replace($root_path, '', $name);
                    }

                    $this->add_data($name.$file, $data);
                }
            }
        }

        return TRUE;
    }
#7

[eluser]InsiteFX[/eluser]
You might be able to chmod them zip them and then chmod them back.

The problem is that web servers hide them because of the .

Mine are set at 644 which will only allow the owner to read/write anyone else only has read access.

It may also be that the zip library is flagging the .

InsiteFX
#8

[eluser]spec[/eluser]
I'm using XAMPP on WinXP; how can I chmod them? In their properties, Hidden checkbox isn't checked
#9

[eluser]InsiteFX[/eluser]
You can not chmod the files on Windows, If you want to see them then your need to change you folder/view settings to allow hidden files.

InsiteFX
#10

[eluser]bubbafoley[/eluser]
[quote author="InsiteFX" date="1299670535"]You might be able to chmod them zip them and then chmod them back.

The problem is that web servers hide them because of the .

Mine are set at 644 which will only allow the owner to read/write anyone else only has read access.

It may also be that the zip library is flagging the .

InsiteFX[/quote]

The zip library only needs read access. It makes a copy of the files and then archives them. The htaccess file isn't being archived because the function read_dir() ignore files starting with a '.'.

spec, here's what you can do to make read_dir() work the way you want.

- Create a file in application/libraries/ called MY_Zip.php
- Override read_dir() to include files starting with a '.'

Ex:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Zip extends CI_Zip {
    
    /**
     * Read a directory and add it to the zip.
     *
     * This function recursively reads a folder and everything it contains (including
     * sub-folders) and creates a zip based on it.  Whatever directory structure
     * is in the original file path will be recreated in the zip file.
     *
     * @access    public
     * @param    string    path to source
     * @return    bool
     */
    function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
    {
        if ( ! $fp = @opendir($path))
        {
            return FALSE;
        }

        // Set the original directory root for child dir's to use as relative
        if ($root_path === NULL)
        {
            $root_path = dirname($path).'/';
        }

        while (FALSE !== ($file = readdir($fp)))
        {
            if ($file == '.' OR $file == '..')
            {
                continue;
            }

            if (@is_dir($path.$file))
            {
                $this->read_dir($path.$file."/", $preserve_filepath, $root_path);
            }
            else
            {
                if (FALSE !== ($data = file_get_contents($path.$file)))
                {
                    $name = str_replace("\\", "/", $path);

                    if ($preserve_filepath === FALSE)
                    {
                        $name = str_replace($root_path, '', $name);
                    }

                    $this->add_data($name.$file, $data);
                }
            }
        }

        return TRUE;
    }
    
}




Theme © iAndrew 2016 - Forum software by © MyBB