Awesome!  I decided to use the Session controller as it seems to work
the best for me since the user has to be logged in (in the future) to
be able to perform these functions. It works now.  Had a slight
problem with the zip file format for some reason.  If I name the file
with a date and/or time in the filename itself, the zip file craps
out.  I am guessing it's the semicolons in the file name used with the
time.  So I just named the file to backup.xml and made the zip file
name dynamically with a date and time.

I like to include my code for other people to look at and use/
critique.  When I have a problem with something, I always like a
demonstration with the explanation so, here it is.  Sorry if it is a
large amount of information.

Here is my resulting code using the flash and session controller:
    /**
     * This function is used to backup the specified tables in the
database and
     * present them as a zip file or regular file and xml or regular
sql depending
     * on what the user has selected.
     * @todo Get the controller to do a flash maybe to forward to
another url
     * after the view so the download will happen
     */
    function database_backup($classifieds = false)
    {
        // If you pass classifieds it will just backup the classifieds
and association tables
        // This is for convenience
        if ($classifieds == 'classifieds')
        {
            $this->zipinfo = $this->AdminTool-
>backup_classified_tables();
            if (!$this->zipinfo['success'])
                {
                $this->set('error', 'Fail');
                $this->render();
                }
                else
                {
                $this->AdminTool->empty_classified_tables();
                }
                if (isset ($this->zipinfo))
                {
                        $this->Session->write('fileinfo', $this->zipinfo);
                $this->flash('Your backup was successful!<br />Your
download will begin in 5 seconds or<br />click here to begin now.', '/
admin_tools/download_file/', 5);
                }
        }
        // This part is where you select which tables you want to
backup
        if (isset ($this->data))
        {
                // We got data back from the form, process it
                $this->fileinfo= $this->AdminTool->database_backup($this-
>data);
                // Test to see if the file export and zipping were
successful
                if ($this->fileinfo['success'] == false)
                {
                $this->flash('ERROR: Could not back up the specified
tables!', '/admin_tools/', 5);
                $this->render();
                }
        }
        else
        {
                // We recieved no data from the form, so present the form
                // Here we privide the list for the checkboxes of the
tables in the database
                $this->dbinfo= $this->AdminTool->getDBInfo();
                $this->set('dbinfo', $this->dbinfo);
                $this->render();
        }

        // The form was processed and the files were backed up and
zipped, forward to the file download
        if (isset ($this->fileinfo))
        {
                // We want to set the path, filename and application type
in the session so when we
                // forward the user, we can read it again.
                $this->Session->write('fileinfo', $this->fileinfo);
                $this->flash('Your backup was successful!<br />Your
download will begin in 5 seconds or<br />click here to begin now.', '/
admin_tools/download_file/', 5);
        }
    }
    /**
     * This is the main function I use for zip and other files to be
downloaded
     * via the admin_tools controller.  It is mainly used for database
backups.
     * The function checks the session information for fileinfo array
to get the
     * path and the name of the file as well as the content type.
Once it checks
     * it initiats a browser download window and exits.
     */
    function download_file()
    {
        if ($this->Session->read('fileinfo'))
        {
            $this->fileinfo= $this->Session->read('fileinfo');
            header('Pragma: public');
            header('Content-type: ' . $this->fileinfo['contenttype']);
            header('Content-length: ' . filesize($this-
>fileinfo['filepath'] . $this->fileinfo['filename']));
            header("Content-Transfer-Encoding: binary");
            header('Content-Disposition: attachment; filename="' .
$this->fileinfo['filename'] . '"');
            readfile($this->fileinfo['filepath'] . $this-
>fileinfo['filename']);
            exit ();
        }
        else
        {
            $this->flash('There was an error downloading the file!', '/
admin_tools/', 5);
        }
    }

Here is the model functions:
    /**
     * This function is used to make backups of the selected tables in
a database
     * @var array The array should be a "cake" array obtained from a
form
     * @return array We return an array here containing success or
failure with the path and filename of the backup
     */
    function database_backup($data)
    {
        $this->tmpBackup= '/var/www/localhost/htdocs/app/tmp/
xml_backup/';
        $this->filename= 'backup';
        $this->zipname = 'data_backup'.date('Y-m-d_H:i:s').'.zip';
        foreach ($data['AdminTool'] as $key => $value)
        {
            if ($key != 'zip' && $key != 'xml' && $value != 0)
            {
                $this->sqlDumpTableString= $this->sqlDumpTableString .
$key . " ";
            }
        }
        if ($data['AdminTool']['xml'] != '0')
        {
            $this->dataDumpFormat= '.xml';
            exec('/var/www/localhost/bin/mysqldump --xml test ' .
$this->sqlDumpTableString . '> ' . $this->tmpBackup . $this-
>filename . $this->dataDumpFormat);
        }
        else
        {
            $this->dataDumpFormat= '.sql';
            exec('/var/www/localhost/bin/mysqldump test ' . $this-
>sqlDumpTableString . '> ' . $this->tmpBackup . $this->filename .
$this->dataDumpFormat);
        }
        if ($data['AdminTool']['zip'] != '0')
        {
            if (file_exists($this->tmpBackup . $this->filename . $this-
>dataDumpFormat))
            {
                $this->zip= new ZipArchive();
                $this->zip->open($this->tmpBackup . $this->zipname,
ZIPARCHIVE :: CREATE);
                $this->zip->addFile($this->tmpBackup.$this->filename.
$this->dataDumpFormat, $this->filename.$this->dataDumpFormat);
                $this->zip->close();
                exec('rm -f ' . $this->tmpBackup . '*.xml');
                exec('rm -f ' . $this->tmpBackup . '*.sql');
                $this->zipinfo['success']= true;
                $this->zipinfo['filename']= $this->zipname;
                $this->zipinfo['filepath']= $this->tmpBackup;
                $this->zipinfo['contenttype']= 'application/zip';
                return $this->zipinfo;
            }
            else
            {
                $this->zipinfo['success']= false;
                $this->zip->close();
                return $this->zipinfo;
            }
        }
        else
        {
            $this->zipinfo['success']= true;
            switch ($this->dataDumpFormat)
            {
                case '.xml' :
                    $this->zipinfo['contenttype']= 'text/xml';
                    break;
                case '.sql' :
                    $this->zipinfo['contenttype']= 'text/plain';
                    break;
                default :
                    $this->zipinfo['contenttype']= 'text/plain';
                    break;
            }
            $this->zipinfo['filename']= $this->filename . $this-
>dataDumpFormat;
            $this->zipinfo['filepath']= $this->tmpBackup;
            return $this->zipinfo;
        }
    }
On Mar 7, 9:10 am, "AD7six" <[EMAIL PROTECTED]> wrote:
> On Mar 7, 5:32 pm, "Christopher E. Franklin, Sr."
>
> <[EMAIL PROTECTED]> wrote:
> > Hrmm, you had a good idea but, I am not figuring out how to implement
> > this.
>
> I do it almost exactly as I described.
>
>  I sat there for an hour yesterday just thinking about it.  My
>
> > problem is that I have a simple array with the path and name of the
> > file.  The file name and paths change dynamically but with flash and
> > redirect, I don't see a way to pass the data to a new page.  is $this->data 
> > global?  I mean if $this->data contains my array, when the page
>
> > gets redirected, it goes bye-bye, yeah?
>
> > Here is the array I am trying to pass:
>
> > $data = array('AdminTool' => array('success' = true, 'filepath' => '/
> > whatever/here/', 'filename' => 'blah_blah_2007-03-06_12:14:03.zip'));
>
> redirect to /whatever/here/blah_blah_2007-03-06_12:14:03.zip
>
> OR redirect to some controller method that uses the passed parameters
> or the session to find the right file to serve.
>
> > I get this array from functions in the model that collect the
> > information for me and zips it.  The model passes the array with where
> > the zip is located and the name of the zip file.  From the controller
> > I could just set the zipinfo variable with the array and access it
> > that way but, the problem comes in where, if I want to display HTML
> > like success here is what info was collected, and once that view
> > loads, have the browser automatically bring up the download box for
> > the zip file.
>
> You'd need to edit your zip generating function (are you using the zip
> component) such that it also generates a list of files it found, and
> returns that on success (for example).
>
>
>
> > This, for me, is more about a "can it be done or not" and to implement
> > it whether it would be useful or not.  It just means that my knowledge
> > will grow.
>
> Sure it can ;).
>
> HTH,
>
> AD


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to