Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Download to local after ftp_get()
Old 03-01-2008, 07:49 PM Download to local after ftp_get()
Extreme Talker

Posts: 199
Trades: 0
I am using ftp_get to transfer a file from a secure location, to a temp location. (working)

Once at the temp location i need to have the file automatically download to the clients local machine. Something like a right click "save as" action. How do i do this? I'm thinking its done with headers but not sure.

My Current Code is below. Thanks for all responses.

[code=text]
// define some variables
$ftp_server = "123.45.67.890";
$local_file = '/scratch1/test/'.$name;
$server_file = $name;

echo "server file: $server_file <br>";

$ftp_user_name = "5a515";
$ftp_user_pass = "m5n5o5";

// 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);

if ($login_result) echo "Successfully connected<br>";

//change directory
ftp_chdir($conn_id, "tts");
ftp_chdir($conn_id, "incoming");

// 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 "<br>There was a problem\n";
}

// close the connection
ftp_close($conn_id);

[/code]
empiresolutions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-01-2008, 10:59 PM Re: Download to local after ftp_get()
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
A modification like this should do the trick...

PHP Code:
<?php
  
if (isset($_GET['action']))
  {
    switch (
$_GET['action'])
    {
      case 
'download':
        if (isset(
$_GET['file']))
        {
          
$file '/scratch1/test/' $_GET['file'];
          if (
file_exists($file))
          {
            
$filetype false;
            if (
$finfo = @finfo_open(FILEINFO_MIME))
            {
              
$filetype = @finfo_file($finfo$file);
            }
            else
            {
              
$filetype = @mime_content_type($file);
            }
            
            if (
$filetypeheader("Content-type: $filetype");
            echo @
file_get_contents($file);
            exit();
          }
        }
        
        
header('HTTP/1.0 404 Not Found');
        exit();
        break;
    }
  }
// define some variables
$ftp_server "123.45.67.890";
$local_file '/scratch1/test/'.$name;
$server_file $name;
echo 
"server file: $server_file <br>";
$ftp_user_name "5a515";
$ftp_user_pass "m5n5o5";
// 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);
if (
$login_result) echo "Successfully connected<br>";
//change directory
ftp_chdir($conn_id"tts");
ftp_chdir($conn_id"incoming");
// try to download $server_file and save to $local_file
if (ftp_get($conn_id$local_file$server_fileFTP_BINARY)){
echo 
"Successfully written to $local_file - Click <a href='" $_SERVER['PHP_SELF'] . "?action=download&file=" urlencode($server_file) . "' target='_blank'>here</a> to download\n";
}else{
echo 
"<br>There was a problem\n";
}
// close the connection
ftp_close($conn_id);
?>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-02-2008, 12:01 AM Re: Download to local after ftp_get()
Extreme Talker

Posts: 199
Trades: 0
mgraphic-

i can see how your idea will work, but i would like the file to automatically download to them. They have all ready chosen a link to start the whole process. Having them click another link to finish the download is 1 click to much.
empiresolutions is offline
Reply With Quote
View Public Profile
 
Old 03-02-2008, 03:17 AM Re: Download to local after ftp_get()
Extreme Talker

Posts: 199
Trades: 0
This is the solutions. Thanks to NogDog.
Code:
// try to download $server_file and save to $local_file 
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)){ 
    header('Content-Length: '. filesize($local_file)); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename="'.basename($local_file).'"'); 
    header('Content-Transfer-Encoding: binary'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    readfile($local_file); // send the file 
    exit;  // make sure no extraneous characters get appended 
}
empiresolutions is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Download to local after ftp_get()
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14274 seconds with 12 queries