Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 5 of 5

Thread: FTP download

  1. #1
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    I tried to serach for this question, but found no threads.

    How do I write a script for downloading files from the ftp-folder on my server.
    I tried to use a function from PHP.NET, but I get errors and can't quite figure out what's wrong. The syntax for the file request should according to PHP.NET look like this:

    PHP Code:
    ftp_get($conn_id$local_file$server_fileFTP_BINARY
    I've tried to define $local_file as 'C:\foldername\subfoldername'
    and
    'C:\foldername\subfoldername\filename' (where filename is the name I want to save the dowloaded file as)

    Is $local_file just a filename you invent, is it supposed to be the path to the directory where you want to save the file, or is it neither of the above and something completely different??

    If you click on a download-link anywhere else on the net, a (Windows)window opens and asks you in which directory you want to save the file. Is that not possible to achieve here?

    I get the ftp connection and everything else to work, but the above mentioned line gives me the following error:
    could not open 'c:\folder\subfolder...'


    Whatever I try, the script halts at the above mentioned line.

    Any suggestions?
    S. Rosland

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Try using Unix-style paths for the local folder (for example, /foldername/subfoldername).
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member skrlin's Avatar
    Join Date
    Apr 2003
    Location
    Illinois
    Posts
    562
    Member #
    1063
    Trying it out on my server I get the same thing.

    PHP Code:
    <?php
    if($local_file and $server_file) {
     
    // define some variables
     // defined in post
     
    $ftp_server 'ftp.myserv.com';
     
    $ftp_user_name '********';
     
    $ftp_user_pass '********';
     
     
    // connect to the FTP server
     
    $conn_id ftp_connect($ftp_server);
     
    $login_result ftp_login($conn_id$ftp_user_name$ftp_user_pass);

     
    // try to download
     
    if (ftp_get($conn_id$local_file$server_fileFTP_BINARY)) {
         echo 
    "Successfully written to $local_file\n";
     } else {
         echo 
    "There was a problem\n";
     }

     
    // close the connection
     
    ftp_close($conn_id);
    } else {
     echo 
    '
     <form action="ftp.php" method="post">
     Choose a file to download:<BR />
      <input type="radio" name="server_file" id="f1" value="file.jpg"
       onchange="document.getElementById(\'file\').value=this.value" />
      <label for="f1">Image</label><BR />
      <input type="radio" name="server_file" id="f2" value="file.htm"
       onchange="document.getElementById(\'file\').value=this.value" />
      <label for="f2">HTM File</label><BR /><BR />
      Local Path: <input type="text" name="local_file" length="30" id="file" /><BR /><BR />
      <input type="submit" name="submit" value=" Download " />
      <input type="reset" name="reset" value=" start over " />
    '
    ;
    }
    ?>
    And I get this:
    Warning: ftp_get() [function.ftp-get]: Error opening file.jpg in /home/virtual/site89/fst/var/www/html/Testing/jpg/ftp.php on line 14
    There was a problem
    or
    file.jpg: No such file or directory in /home/virtual/site89/fst/var/www/html/Testing/jpg/ftp.php on line 14
    Note: I'm using Linux on the server and client. I haven't tried this in windows yet.
    - Brian

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Hi,

    Tried the Unix-style '/folder/foldername/file' approach, but I stille get the same warning.
    S. Rosland

  6. #5
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    make jpg files downloadable

    Came over a code another place on the net (can unfortunately not remember where).
    I moved some image files from my ftp site to my "public_HTML" site.
    (Only reason was to bypass the login script to the FTP as I haven't figured out how to make anonymous access possible)

    The problem with using an anchor <a href.. to point to a file a browser recognizes, is that instead of opening a dialog box for download (as it would if it encountered an unknown file-extention), it opens the file in a browser-window. Files like JPEG, GIF, PNG etc, just opens up when you point at them.

    Instead of pointing directly to the file in question, I made a pointer to a file I named "downloadable.php" containing the following script:

    PHP Code:
    <?php
    $fp 
    fopen("../images/download/$image","r");
    $buff fread($fp,1500000);
    Header("Content-Type: image/jpg");
    // you can put the header file-length here, but it's not neccessary
    Header"Content-Disposition: attachment; filename=$image");
    echo 
    $buff;
    ?>
    Of course you would have to enclose the <a href..> tag in the referring document in a <form> with hidden fields containing the image name and POST-ing it as $image.

    When you click on one of these links, the familiar download-dialogbox appears asking you where you want to store the image.
    S. Rosland


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:17 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com