Forum Moderators: coopster

Message Too Old, No Replies

use PHP for non-anonomous FTP downloads

User FTP downloads using PHP

         

cphillips

2:19 pm on Aug 19, 2005 (gmt 0)

10+ Year Member



Hi,

I am having a problem creating a simple ftp download for users. For some files it works, and for others I get a long pause.

The final goal is to have a form button, when clicked, behind the scenes it logs into ftp, and gets the file. I then pops up a download box (the standard save as.. box)

I need the user to not know what the path is, and I want to not allow anonomous FTP. Is there an easier way to achieve this?

Ok so on an HTML page I have a button (in form tags) that uses method="post" and the action calls the PHP page (code below).

Here is the code:
<?php
// Set time limit to infinite
set_time_limit(0);

// Define server and target directory
$ftpServer = 'new.principiaproducts.com';
$targetDir = 'classic/';
$fileName = 'rcotour.exe';
$mimeType = 'application/exe';
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') or
strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7')) {
$mimeType = 'application/x-download';
}

// Connect to server
if (!$fp = ftp_connect($ftpServer, 21, 30)) {
die('Connection Failed');
}

// Login
if (!ftp_login($fp, 'demo', 'password')) {
die('Login failed');
}

// Change directory
if (!ftp_chdir($fp, $targetDir)) {
die ('Unable to change directory to: ' . $targetDir);
}

// Tell the browser its a file for downloading
header('content-disposition: attachment; filename=' . $fileName);
header('content-type: ' . $mimeType);
header('content-length: ' . filesize($fileName));

//Display the file
readfile($fileName);
?>

Any Ideas?

Thanks in advance!

jatar_k

8:57 pm on Aug 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you looked at the server itself to see if there are any errors on any of the longer downloads?
is it related to filesize?