Hi there.
I am trying to write a site that has some very basic FTP functions. Basically that site lets a client download files from an FTP server via a web interface. Nothing special, just downloading.
So far I have some rough test code that will connect to the ftp server list the files in a specific folder that provide html links that the client can click on to download the file.
I need the browser to download the files rather than displaying them (ie. downloads an image to the hardisk via 'save as...' rather than just displaying it).
At the moment my code will download the file but it is corrupt. After some investigation using simple text files I found that the saved file contains not only the file content but the HTML output too which is obviously why the files become corrupt.
I am prett sure that it has something to do with the headers used. If somebody can have a look over the code and the text file I'd be grateful.
Sorry for the long post!
Cheers, Steve
Here's the code I am using.
<HTML>
<HEAD>
</HEAD>
<BODY>
<?php
// filesize functions
function truncate_decimals($num) {
$shift = pow(10, 1);
return ((floor($num * $shift)) / $shift);
}
function compute_size($byte_number) {
if ($byte_number < 1024) {
return $byte_number. 'bytes';
} elseif ($byte_number < 1048576) {
return truncate_decimals($byte_number / (1024)). 'KB';
} elseif ($byte_number < 1073741824) {
return truncate_decimals($byte_number / (1048576)).' MB';
}
}
//set variables
$ftp_server = "yourserver";
$user = "youruser";
$pass = "yourpass";
// connect to ftp server
$connection = ftp_connect($ftp_server);
// login to server
$login_result = ftp_login($connection, $user, $pass);
ftp_pasv($connection, true);
// check connection
if ((!$connection) || (!$login_result)) {
echo "FTP connection has failed";
exit;
} else {
echo "Connected to $ftp_server, for user $user<br>\n";
}
// download file
if (isset($_GET["download_file"]))
{
echo "you have chosen to download ".$_GET["download_file"]."\n";
$size = $_GET['download_file'];
$handle = fopen("ftp://$user:$pass@$ftp_server/".$_GET["download_file"], "rb");
// Send headers
header("Cache-Control: ");
header("Pragma: ");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$_GET["download_file"]."\"");
header("Content-Length: $size");
header("Expires: 0");
//send the file
fpassthru($handle);
}
// get folder contents
function itemize_dir($contents) {
foreach ($contents as $file) {
if(ereg("([-dl][rwxstST-]+).* ([0-9]*) ([a-zA-Z0-9]+).* ([a-zA-Z0-9]+).* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9])[ ]+(([0-9]{2}:[0-9]{2})|[0-9]{4}) (.+)", $file, $regs)) {
$type = (int) strpos("-dl", $regs[1]{0});
$tmp_array['line'] = $regs[0];
$tmp_array['type'] = $type;
$tmp_array['rights'] = $regs[1];
$tmp_array['number'] = $regs[2];
$tmp_array['user'] = $regs[3];
$tmp_array['group'] = $regs[4];
$tmp_array['size'] = $regs[5];
$tmp_array['date'] = date("d-m-y",strtotime($regs[6]));
$tmp_array['time'] = $regs[7];
$tmp_array['name'] = $regs[9];
}
$dir_list[] = $tmp_array;
}
return $dir_list;
}
$buff = ftp_rawlist($connection, "/");
$items = itemize_dir($buff);
foreach ($items as $item)
{
$filename = $item['name'];
$filesize = compute_size($item['size']);
$filesizeb = $item['size'];
$filedate = $item['date'];
// try to ftp a file
//$handle = fopen("ftp://$user:$pass@$ftp_server/$filename", "rb");
//print file names note. this refers back to itself
echo "<a href=\"ftptester?download_file=$filename&download_size=$filesizeb\">$filename</a> - $filesize - $filedate<br>\n";
}
// close the FTP connection
ftp_close($connection);
?>
</BODY>
</HTML>
Here is the resulting file when trying to download a simple text file.
<HTML>
<HEAD>
</HEAD>
<BODY>
Connected to 82.45.191.225, for user whookam<br>
you have chosen to download test.txt
******* THIS IS THE TEXT FILE THAT WAS DOWNLOADED ********
This file will be appended to the HTML output and saved and opened in the text editor.
******* END OF TEXT FILE *******<a href="ftptester?download_file=test.txt&download_size=191">test.txt</a> - 191bytes - 09-03-05<br>
</BODY>
</HTML>