SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to ftp a zip file and unzip it on the server

    Here is what I need to do using php:
    • upload a zip file (e.g. foo.zip) to the server; the zip file contains files and directories
    • unzip the zip file on the server with the owner of the unzipped files and directories ending as ftp user (so they can be later deleted using regular FTP client)

    Please note that permissions on the directory where the zip file is uploaded are 755 and the owner of the directory is ftp user.

    Here is how I attempted to do it:
    • uploaded the zip file using 'ftp_put'
    • uploaded 'unzip.php' file using 'ftp_put'
    • called 'unzip.php' using curl; 'unzip.php' file is using ZipArchive::extractTo to unzip the file. The problem I am having with this approach is that the user the php script is running under do not have 'write' permission to the directory so 'extractTo' function fails...

    Any help will be appreciated.
    www.forkaya.com - Web Development, PHP Scripting

  2. #2
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I faced a similar situation to what you are describing a few months back on a system project that required auto downloading and parsing files. I ended up putting together a small application that did the job.

    If you would like I can send you the files and instructions on setup. Hopefully it will be of some use to you.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the offer. Is it written in php? If yes I would definitely like to see the code (if possible).
    www.forkaya.com - Web Development, PHP Scripting

  4. #4
    SitePoint Enthusiast vessio's Avatar
    Join Date
    Jan 2009
    Location
    Houston, Texas
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, I will send you a PM with a download link.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vessio's solution looks good but requires to manually change access permissions on the target server and this is what I am trying to avoid from maintenance and security point of view. The zip file will be uploaded to multiple servers... So for right now the only solution I can think of is to ftp every file in the zip file separately. It will work but will be much slower than 'ftp'ing zip file and unzipping on the target server. so I am still open for any advice.
    www.forkaya.com - Web Development, PHP Scripting

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another solution is to install ftp server that has post-upload hooks. Basically you can configure the server to execute a script after the upload is complete.
    I used pure-ftp server for exactly this type of task.
    http://www.pureftpd.org/project/pure-ftpd
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it looks like I am stuck with uploading file by file for right now... so here it the function I created to do it:

    PHP Code:
    function ftp_dir_upload($conn,$path2dir){
        
    $dir dir($path2dir);
        while((
    $file $dir->read()) !== false) {
            
            if(
    is_dir($dir->path.$file)) {
                if ((
    $file == '.') or ($file == '..')) {
                    continue;
                }
                if (!
    ftp_mkdir($conn,$file)) {
                    
    $dir->close();
                    return 
    false;
                }
                if (!
    ftp_chdir($conn,$file)) {
                    
    $dir->close();
                    return 
    false;
                }
                
                
    ftp_dir_upload($conn,$dir->path.$file.'/');

                if (!
    ftp_chdir($conn,'..')) {
                    
    $dir->close();
                    return 
    false;
                }
            } else {
                if (!
    ftp_put($conn,$file,$dir->path.$file,FTP_BINARY)) {
                    
    $dir->close();
                    return 
    false;
                }
            }
        }
        
    $dir->close();
        return 
    true;

    It is pretty slow because I need to transfer 726 files...
    www.forkaya.com - Web Development, PHP Scripting

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2008
    Location
    New York
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lampcms.com View Post
    Another solution is to install ftp server that has post-upload hooks. Basically you can configure the server to execute a script after the upload is complete.
    I used pure-ftp server for exactly this type of task.
    http://www.pureftpd.org/project/pure-ftpd
    Good suggestion... The only problem is that both the base and target servers are out of my control. Scripts are running on hosted accounts.
    www.forkaya.com - Web Development, PHP Scripting

  9. #9
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    You can try Wing FTP Server, it supports zip/unzip on the server side perfectly.

    And you can download it from: www.wftpserver.com/download.htm


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
  •