tags:

views:

35

answers:

2

hey guys, i really really need your help. I'm successfully setting up a connection to my ftp server. However I can't figure out how I can download a dynamic file from my server. I guess this would help a lot of other people as well cause i couldn't find a tutorial or explanation anywhere on the web.

I'm simply listing all of my files on my ftp. If i click on one i call download.php which connects again to the server and should download the file to my harddrive. I was able to auto-prompt a download window and a file gets downloaded to my hdd. however only a damaged file where no headers are set.

/* path to files on ftp server … e.g. /folder/folder/name_of_file.jpg */
$p_arr = explode("/", $path);
/* the file name … e.g name_of_file.jpg */
$file = end($p_arr);

//$finfo = finfo_open(FILEINFO_MIME_TYPE); //returns a fatal error - function not found 
//$mimetype = finfo_file($finfo, $file);
//finfo_close($finfo);

//filetype($file)

/* creating a temporyry file to save to */
$tempFile = tempnam("/tmp", "FOO");

if(ftp_get($conn_id, $tempFile, $file, FTP_BINARY)){ 
    /*header Content-type: must be dynamic*/
    //header('Content-type:' . $mimetype);

    /*header to auto_prompt download window*/
    header('Content-Disposition: attachment; filename="'. $file .'"');
    readfile($tempFile);
} else { 
    echo "There was a problem <br>";
    echo $file . "<br>"; //e.g. image.jpg
}

/* deleting the file after the process */
//unlink($tempFile);

1.) i can't figure out why ftp_get returns false. The connection gets set up and the file exists in the right directory. 2.) i don't know how i can find out the mimetype of the file on the server and give it the downloaded file, so it's not damaged.

please help me out here, i'm really stuck. thank you in advance

A: 

Try to save it locally first and then push it back to browser.

Use that code to save your file locally.

<?php
                // define some variables
        $folder_path = "YOUR FOLDER PATH";
        $local_file = "LOCAL FILE PATH";
        $server_file = "SERVER FILE PATH";

        //-- Connection Settings
        $ftp_server = "IP ADDRESS"; // Address of FTP server.
        $ftp_user_name = "USERNAME"; // Username
        $ftp_user_pass = "PASSWORD"; // Password
        #$destination_file = "FILEPATH";

        // set up basic connection
        $conn_id = ftp_connect($ftp_server);

        // login with username and password
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

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

        // close the connection
        ftp_close($conn_id);
?>

I also found this, maybe it could help you

My FTP-Server always responded "bool(false)" instead of presenting me a directory-listing. I had to add ftp_pasv($conn_id, true); right after the $login_result = ftp_login(...); line. After that it just worked fine.

Philippe
what do you mean? how locally?
Looks like the original code is already saving it locally (or trying to), in the tmp folder.
banzaimonkey
yep, exactly! that's why i didn't get it! don't get the thing with saving it locally. where should i save the file locally if i don't now anything about the user's local path. i just want to list my files from my server, on click download them to their harddrive. nothing else. i want to get the right headers from the file on the server and pass them along. and i wonder why my connection gets setup successfully but ftp_get returns "There was a problem". Propably due to the fact that i don't know how to save it to a temp-file.
Just to make sure everything works fine with ftp_get(). It seems there's a lot of commented stuff in his example.
Philippe
Make sure /tmp exists on your server where php is running
Philippe
Are the files on the same server as PHP? If so, you don't need to FTP. "Locally" refers to the assumption that PHP and the source files are on two separate servers (the user is a third computer).
banzaimonkey
how can i then handle a forced download. $file stores the full path to my file on the server. i want it to download to the hdd. and do I really have to create a /tmp directory if i'm using ftp_get. when yes, where?
A: 

tempnam()

When I used tempnam() it changed the file extension to .tmp. This may cause problems, so I don't recommend doing it this way. At the very least, you should change the extension back to what it was in the original filename.

You can achieve this using pathinfo() in conjunction with regexp and rename(), after you've ftp'ed the file.

mimetype

Regarding mimetype: You only need this information if you want the browser to display the file you send to the user. For downloads, the browser only needs to know it's a binary file, and the browser or user can guess from the extension.

If you still want to check the mimetype, you'll have to wait until the file is on your server, so the mime-checking code will go inside the if(ftp_get()) block.

ftp

You haven't shown your connection code (so I'm assuming), but you will need the full path for the file you're pulling from FTP. If the ftp_get() step is failing, you're probably requesting an invalid file.

Otherwise, you will have to ftp_chdir() after connecting and logging in so you're in the correct remote folder.

code

This code worked for me:

<?php
$ftp = ftp_connect('server');
ftp_login($ftp, 'username', 'password');
$file = '/media.banzaimonkey.net/images/forums/boasas_banner_07.gif';

$tempFile = tempnam("/tmp", "FOO");

if(ftp_get($ftp, $tempFile, $file, FTP_BINARY)){
    echo 'success!';
} else {
    echo 'ffffail';
}
?>

If that code doesn't work for you, you may have a problem with permissions, configuration, or some other environment-specific issue.

banzaimonkey
do I have to create a /tmp directory on my server in order to work. where?