Using PHP to ftp a Zip file after creating it

Posted on 2005-04-22
Last Modified: 2008-11-18
The user opens the application on the browser and the system automatically goes to a predefined folder and uploads all the files in the folder without the user having to select the files. Next the application creates a zip file of all the files in the entire folder
Next this zip file is sent by ftp to an ftp server

I am trying to use the file upload withinn the dir object but so far has been unsuccessful. I dont have an idea on how I can create the zip file and then ftp it

With regards

The code is as follows -

      //Constant parameters defined
      $upload_dir = "../images/";
      $upload_url = "";
      $mydir = dir('c:/images/');
      while(($file = $mydir->read()) !== false)
          echo "Filename: $file<BR>";
            $file_path = $upload_dir.$file;
            move_uploaded_file($file, $file_path);


Question by:chaiti_c
    LVL 7

    Accepted Solution


    Here's the links to the free zip/unzip program.

    $username = 'your_ftp_useername'; // your ftp username
    $password = 'your_ftp_password';

    shell_exec("zip myfiles.*");

    $ftpr= ftp_connect('');

    ftp_login($ftpr, $username,$password);

    ftp_put($ftpr, $uploadir, $mydir.'',FTP_BINARY);


    LVL 7

    Expert Comment

    Although this is largely untested code, I think the theory is sound.

    Have you tried it? Does it work for your purposes?

    Author Comment

    Thanks a lot.
    I am trying it out. Please bear with me
    I will be getting back as soon as possible
    With regards
    LVL 7

    Expert Comment

    DOn't forget, if at first it can't transfer files, use ftp_pasv().

    Author Comment

    Thanks for your help

    Its working I am using pkzip to zip the file on my development server whose OS is Windows 2000. My production server's OS is Linux. Can you please suggest how i zip on the Linux server

    Thanking you once again for your help
    With regards
    LVL 7

    Expert Comment

    This I know best !

    Infozip should be on your box already.

    The syntax is remarkably similiar to the infozip I gave you the link to. (Why do you think I recommended it? It's free unlike PKZIP dos.).  

    Step one will be locating the unzip program on your *nix box.

    -bash-2.05b$ which unzip

    So the program is in /usr/bin/unzip, eh?

    "unzip data1" Would unzip all files to the current, however, in your case, I'd reccomend /usr/bin/unzip -o myzipfile.

    UnZip 5.50 of 17 February 2002, by Info-ZIP.  Maintained by C. Spieler.  Send
    bug reports to the authors at; see README for details.

    Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
      Default action is to extract files in list, except those in xlist, to exdir;
      file[.zip] may be a wildcard.  -Z => ZipInfo mode ("unzip -Z" for usage).

      -p  extract files to pipe, no messages     -l  list files (short format)
      -f  freshen existing files, create none    -t  test compressed archive data
      -u  update files, create if necessary      -z  display archive comment
      -x  exclude files that follow (in xlist)   -d  extract files into exdir

    modifiers:                                   -q  quiet mode (-qq => quieter)
      -n  never overwrite existing files         -a  auto-convert any text files
      -o  overwrite files WITHOUT prompting      -aa treat ALL files as text
      -j  junk paths (do not make directories)   -v  be verbose/print version info
      -C  match filenames case-insensitively     -L  make (some) names lowercase
      -X  restore UID/GID info                   -V  retain VMS version numbers
                                                 -M  pipe through "more" pager
    Examples (see unzip.txt for more info):
      unzip data1 -x joe   => extract all files except joe from zipfile
      unzip -p foo | more  => send contents of via pipe into program more
      unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer

    If you dont' posses it, you can get it from

    Also, unix (Linux) variant is here:

    Pretty simple install. Precompiled binaries are available if you know which you need, glibc or libc5.


    Author Comment

    Thanks one again I am going to try it out. This application would not have been possible ithout your help
    With regards

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now