Download an FTP file via PHP.
#1

Hey. I have a php script for players to download their 'maps' from my server, via FTP. I've got the ftp connecting and logging in, but I'm unsure about having them download the files. I've been googling and stuff and read about ftp_get, but it says it downloads as a local file.

Does this mean I download it as a local file, then re-direct them to that url, then delete the file? If so could I get some guidance? Thanks.

P.S. New to php.

EDIT: Eh, just making it up as I go along, added the ftp_get shit but getting this:

Quote:

Connected as xxx@x.x.x.x
Warning: ftp_get() [function.ftp-get]: The system cannot find the file specified. in /xxx/mapload.php on line 62
There was a problem downloading the file. /scriptfiles/maps/James_1.txt

This is the relevant php code:

PHP код:
            $resultPath "/scriptfiles/maps/" $_POST['pname'] . "_" $_POST['slot'] . ".txt";
            
$ftp_server "...";
            
$ftp_user "...";
            
$ftp_pass "...";
            
            
// set up a connection or die
            
$conn_id ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 
            
            
// try to login
            
if (@ftp_login($conn_id$ftp_user$ftp_pass)) echo "Connected as $ftp_user@$ftp_server\n";
            else echo 
"Couldn't connect as $ftp_user\n";
            
$local_file 'map.txt';
            
$server_file '$resultPath'// Also tried without apostrophes (')
            
if (ftp_get($conn_id$local_file$server_fileFTP_BINARY)) echo "Successfully written to $local_file\n";
            else echo 
"There was a problem downloading the file.\n";
            
            
// close the connection
            
ftp_close($conn_id); 
Reply
#2

you can use this function copy.


PHP код:
<?php
$file 
'example.txt';
$newfile 'example.txt.bak';
if (!
copy($file$newfile)) {
    echo 
"error to copy file: $file...\n";
}
?>
Reply
#3

I need to download the file from FTP, to allow the user to download it. I doubt that's what your code does.
Reply
#4

Код:
<?php
	
	$ftp_host = "serverhostname ex: could be a ip address"; //put the ip / hostname to your ftp here
	$ftp_user = "ftp login"; //put your login here
	$ftp_pass = "ftp password"; //put your password here
	
	$con = ftp_connect($ftp_host) or die("Couldnt connect to your ftp (check your settings!");
	ftp_login($con, $ftp_user, $ftp_pass);
	ftp_get($con, "/scriptfiles/maps/", FTP_ASCII);
	ftp_close($con);
	
	$file = fopen("/scriptfiles/maps/", "r");
	fclose($file);
	
?>
Not sure if it works.
Reply
#5

Your syntax is wrong: http://php.net/manual/en/function.ftp-get.php
Reply
#6

Код:
<?php
	
	$ftp_host = "serverhostname ex: could be a ip address"; //put the ip / hostname to your ftp here
	$ftp_user = "ftp login"; //put your login here
	$ftp_pass = "ftp password"; //put your password here
	$mapfile = "/scriptfiles/maps/";
	
	$con = ftp_connect($ftp_host) or die("Couldnt connect to your ftp (check your settings!");
	ftp_login($con, $ftp_user, $ftp_pass);
	ftp_get($con, "/scriptfiles/maps/, $mapfile, FTP_ASCII);
	ftp_close($con);
	
	$file = fopen("/scriptfiles/maps/", "r");
	$maps = fread($file, filesize("/scriptfiles/maps/"));
	fclose($file);
	
	echo nl2br($maps);

?>
What about now ;d
If not then I suck
Reply
#7

I'm not at the downloading part yet, I need to get the file from FTP first!
Reply
#8

Like garu posted, you can download the file with headers via ftp
Like:
PHP код:
<?php 
header
("Content-disposition: attachment; url=the ftp url here with file location and file name");
header('Content-type: text/plain'); 
readfile($myvariable); 
?>
Should look something like this, and will not show the ftp user/password on the website, will show like ,you're downloading a file from php page, don't show external links or something (:
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)