SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Virtual Directories Path

    Okay! Got a little problem here.

    I have a virtual directory system setup like this (sql table):

    table: folder
    id int auto_inc primary_key
    title text
    parent int (id of this table 0 if root)

    I'm making a backup feature.. and it seems to be messing up.

    Here's my code ignore the messy crappy code:

    PHP Code:
    <?php
    /**
     * ZIP Backing Up
     * TODO: clean code, messy. bad. ugr.
     */
    class AXE_Backup extends Model
    {

        private 
    $_parent_folder// main folder that we start with
        
    private $_parents// this is the parents, it goes up. | bottom -  blah/blah/home/ - top |

        
    function __construct()
        {
            
    parent::Model();
            
    $this->load->library('zip');
        }


        
    /**
         * Main process to tie the check, processing, and downloading together.
         */
        
    public function backup($main_folder)
        {
            
    $this->_parent_folder $main_folder// sets the top level as this one.

            
    if (!$this->_check()) {
                
    show_error('There were no files to backup in this directory.');
                return 
    false;
            }

            while (
    $this->_process_folder($main_folder)) {
            } 
    // starts the process
            
    $this->_download(); // downloads zip
        
    }

        
    /**
         * Checks to see if there are any files on the account, and in the folder.  Saves us time. 
         */
        
    private function _check()
        {
            if (
    $this->db->where('uid'$this->dx_auth->get_user_id())->from('uploads')->
                
    count_all_results() != 0) {
                
    // okay there were at least files on the account..
                
    if ($this->db->where('folder'$this->_parent_folder)->from('uploads')->
                    
    count_all_results() != 0) {
                    return 
    true;
                } else {
                    return 
    false;
                }
            } else {
                
    // there were no files in this account.
                
    return false;
            }
        }

        
    /**
         * Gets the parents/directory structure of that folder.
         */
        
    private function _get_parents($folder_id)
        {
            
    $count $this->db->where('id'$folder_id)->from('folder')->count_all_results();

            if (
    $count != 0) {
                
    $result $this->db->select('parent')->where('id'$folder_id)->get('folder')->
                    
    result();
                
    $result $result[0];

                if (!
    in_array($this->_parents[$folder_id], $this->_parents)) {
                    
    $this->_parents[$folder_id][] = $result->parent;
                }
                
    $this->_get_parents($result->parent);
            }

        }

        
    /**
         * Zips everything up and puts files into directory.
         */
        
    private function _process_folder($folder_id)
        {

            
    $this->_get_parents($folder_id);

            
    $directory '';


            foreach (
    $this->_parents as $blah) {
                if (
    $blah[0] == 0) {
                    
    $directory .= '';
                } else {
                    
    $directory .= $this->axe_folder->get_folder_name($blah[0]) . '/';
                }
            }

            if (
    $folder_id != 0) {
                if (
    $folder_id != $this->_parent_folder) {
                    
    $directory .= $this->axe_folder->get_folder_name($folder_id) . '/';
                }
            }

            foreach (
    $this->db->where('folder'$folder_id)->where('uid'$this->dx_auth->
                
    get_user_id())->get('uploads')->result() as $row) {
                
    $this->zip->add_data($directory $row->filenamefile_get_contents($this->
                    
    config->item('upload_url') . $row->filename));

                
    //if ($row->is_image) {
                //    $this->zip->add_data($directory . "{$row->rawname}_thumb{$row->extension}",
                //        file_get_contents($this->config->item('upload_url') . "{$row->rawname}_thumb{$row->extension}"));
                //}

            
    }

            
    //$this->_parents = "";

            
    foreach ($this->db->where('parent'$folder_id)->where('user'$this->dx_auth->
                
    get_user_id())->get('folder')->result() as $row) {
                
    $this->zip->add_dir($directory $this->axe_folder->get_folder_name($row->id));
                
    $this->_process_folder($row->id);
            }
        }

        
    /**
         * Sends the ZIP to the browser to download.
         */
        
    private function _download()
        {
            
    // add text file, etc.
            
    $time time();

            
    $file = @file_get_contents(base_url() . "_media/folder_template.txt");

            
    $file str_replace("{username}"$this->dx_auth->get_username(), $file);
            
    $file str_replace("{time}"$time$file);
            
    $file str_replace("{folder_id}"$this->_parent_folder$file);

            if (
    $this->_parent_folder == 0) {
                
    $name '/';
            } else {
                
    $name '/' $this->axe_folder->get_folder_name($this->_parent_folder);
            }

            
    $file str_replace("{folder_name}"$name$file);

            
    $this->zip->add_data('README.txt'$file);

            if (
    $this->_parent_folder != 0) {
                
    $folder $this->axe_folder->get_folder_name($this->_parent_folder) . "-";
            }
            
    $this->zip->download("AXE-{$folder}{$this->dx_auth->get_username()}-{$time}.zip");
        }
    }
    And here is how it produces the directory (in _process_folder)


    images/
    images/site/
    images/site/header/
    images/site/site/test/
    images/site/site/images/hey/

    it should be

    images/
    images/site
    images/site/header
    images/site/test
    images/hey

    and it seems to be producing it like that..

    Here is the output.. $this->_parents
    Array
    (
    [86] => Array
    (
    [0] => 0
    )

    [90] => Array
    (
    [0] => 86
    )

    [91] => Array
    (
    [0] => 90
    )

    [92] => Array
    (
    [0] => 90
    )

    [93] => Array
    (
    [0] => 86
    )

    )

    directory: images/site/site/images/hey/

    anyone know a way to fix this? So that it produces the correct directory structure and quits putting files in the wrong directory..
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  2. #2
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry to do this.. but *bump*
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    just a guess

    change
    if ($this->_parent_folder == 0) { $name = '/';

    to

    if ($this->_parent_folder == 0) { $name = '';
    What I lack in acuracy I make up for in misteaks

  4. #4
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's just going to stop putting slashes in it.. not stopping the duplication of folder names ;P
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  5. #5
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I totally don't know why it is happening from looking at the code and the bandaid fix would be to preg_replace any second occourance of "site"

    Since it was a bump I thought I would shoot a guess.

    If no answer I will attempt a regex tomorrow, it's late here now.

    Time to pull out the ilovejackdaniels.com regex cheat sheet. (hint)
    cheers
    What I lack in acuracy I make up for in misteaks

  6. #6
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your problem is in the foreach(). I don't know anything about the axe_folder class, but on each iteration through the foreach, you should echo the result of get_folder_name($blah[0]) to see what it is returning.
    i.e.
    PHP Code:
            foreach ($this->_parents as $blah) {
                if (
    $blah[0] == 0) {
                    
    $directory .= '';
                } else {
                    echo 
    $blah[0].' => '.$this->axe_folder->get_folder_name($blah[0]).'<br>';
                    
    $directory .= $this->axe_folder->get_folder_name($blah[0]) . '/';
                }
            } 
    If what i'm thinking is right, your _get_parents() method is returning too much and causing you to loop through too many directories (thus causing too many directories to get appended to the path).
    Mind the gap.

  7. #7
    SitePoint Zealot CalebMingle's Avatar
    Join Date
    Jul 2007
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the get_folder_name just returns the plaintext name of the folder (like images, documents, etc).

    It should do this:

    /
    - put all files from / into zip.

    /files - first parent
    - put all files from /files into /files of zip
    /files/blah - first child
    - put all files from /files/blah into /files/blah of zip
    /files/blah/test - first child
    - put all files from /files/blah/test into /files/blah/test
    /files/test - second child
    - put all files from /files/test into /files/test
    /test - second parent
    - put all files from /test into /test.

    If you see what I mean?
    :: Caleb Mingle (@dentafrice)
    :: PHP Developer.
    :: g+: http://gplus.to/mingle

  8. #8
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand what you're trying to achieve, what i meant to say is i don't know the finer details of the classes you are using.

    I also know string concatenation and array issues. If the array you posted earlier was the array being looped through for the last directory (what should be "images/hey"), then it should be obvious what the problem is. But again, i don't know the finer details of the classes you are using, so i can't say for sure, which is why i wanted you to run the script with the echo statement i added. Have you tried that? Do you have any results from it?

    Sorry if any of this sounds condescending in anyway, i have a bad habit of making things sound worse than i mean them to.
    Mind the gap.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •