SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    copy files on server on request

    anyone came accross a script such as this...?

    1 clicking a link on a page activates the script,

    2 the script copies a file (defined by this particular link) from public_ftp/subdirectory to public_html/subdirectory,

    3 begins download of that file (the copy sitting in public_html)

    4 ...this is optional but if can be done, it would make my day... deletes the copied file from public_html once the download is complete

    the twist here is the fact that source directory needs to be reached by going up the directory structure, into public_ftp folder which is parallel to public_html folder where the php code would normally reside

    thanks

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Couldn't you do a fopen on the original file in the public_ftp/subdirectory folder and echo that to the browser?

    That would seem a lot easier. Less worrying about permissions, less worrying about rogue files hanging around being nasty to poor stylesheets. Ahem.

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Even better you can use readfile(). Don't forget to set the correct headers.
    Saul

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as far as how to reach that dir

    ../public_ftp/filename.ext

    the ../ will tell it to go up 1 directory, and start the path from that point

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried ../ and that didn't seam to work

    as for fopen and readfile() I don't have enough experience with php to spit out this code

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; filename=filename.ext");
    header("Content-Length: ".filesize("../public_ftp/filename.ext")); 
    readfile("../public_ftp/filename.ext"); 
    Saul

  7. #7
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    google on force downloading a file using PHP

  8. #8
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon
    Try this:

    PHP Code:
    header("Content-Type: application/download");
    header("Content-Disposition: attachment; filename=filename.ext");
    header("Content-Length: ".filesize("../public_ftp/filename.ext")); 
    readfile("../public_ftp/filename.ext"); 

    I'm guessing I should be swapping "filename.ext" with the name of my file...
    it gives me errors such as this:

    Warning: Cannot modify header information - headers already sent by (output started at /home/[MYLOGIN]/public_html/test3.php:2) in /home/[MYLOGIN]/public_html/test3.php on line 4

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You cannot send any output before the header function. And since you are downloading file, you can't send any outup after readfile too. What's at line 2 in your script?
    Saul

  10. #10
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, you're right, I had an empty line of code, that did the damage. Thank you.
    I'm testing this whole concept in my particular case. Will let you know of the results.

  11. #11
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that works well, thanks again

    could the headers be fed variables though...
    so far i've been testing this with this script saved in its own file and called by a link from a different php file.

    what i'm saying is, this is part of a larger project, a php script that acquires file list from the ftp server and displays it for download. Obviously the list is dynamic and so the file names change. That is why I'd have to have reference points in the headers rather than set directories and filenames.

    I tested putting variables in the script you have but I don't think it likes that

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes, you can use vaiables just like in any other strings. Ex.:

    PHP Code:
    $path="../public_ftp";
    $filename="filename.ext";
    header("Content-Type: application/download"); 
    header("Content-Disposition: attachment; filename=$filename"); 
    header("Content-Length: ".filesize("$path/$filename")); 
    readfile("$path/$filename"); 
    Saul

  13. #13
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, I did indeed find an error in my code that was causing the problem. I actaully decided to pass the variables in the url from the page that contains the link to this php script.

    I got it to work 75% of the way, if you could help me out with this final issue, I'll leave you alone already

    the variables that I'm talking about contain files posted on FTP site. The way my existing code lists those is: getting the file list from the server, assigning those to an array, removing "." and ".." from the array (those obviously get there from FTP site), flipping the list so that a file named with highest number in its name lists first, and displaying the directory.

    PHP Code:
    $ftpStream ftp_connect("ftp.server.com");
    $loginResult ftp_login($ftpStream"user""password");
    if (
    $loginResult) {
       
    $dir "project1";
       
    $files_array ftp_nlist($ftpStream$dir);
       
    array_shift($files_array);
       
    array_shift($files_array);
       
    krsort($files_array);
       
    $recentRev current($files_array);
       echo 
    "... here the information is formated into a table with a link in there passing a variable for the current topmost file:
    <a href='../test7.php?Vrb=
    $recentRev'>HTTP</a>"
    ftp_quit($ftpStream); 
    my question here is how do I squeeze the remaining file names (whose number will varry) out from the array so that I can assign them to variables (if neccesary) and pass through with the url...?

  14. #14
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a word of caution.

    if your going to be using a variable for the $filename, you need to be very carefull if this variable comes from the user, for example, through the query string.

    they could do

    script.php?filename=../../../some_file_with_stuff_they_shoudlnt_see!!!

    at the very least use basename() on the filename to prevent directory traversal attacks. however, you would be much better off having a list of filenames they can choose from, and making sure the value exists in your list, which could be a simple table in a database.

  15. #15
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I got it right, what you want to do is to loop through the files:
    PHP Code:
    $ftpStream ftp_connect("ftp.server.com"); 
    $loginResult ftp_login($ftpStream"user""password"); 
    if (
    $loginResult) { 
       
    $dir "project1"
       
    $files_array ftp_nlist($ftpStream$dir); 
       
    array_shift($files_array); 
       
    array_shift($files_array); 
       
    krsort($files_array); 
       foreach(
    $files_array as $recentRev){
         
    $recentRev=basename($recentRev);
         echo 
    "<a href='../test7.php?Vrb=$recentRev'>$recentRev</a><br />";
       }
    ftp_quit($ftpStream); 
    Saul

  16. #16
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help


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
  •