akrytus Posted August 17, 2006 Share Posted August 17, 2006 I searched everywhere, I posted this before and no one even replied. Is it possible nobody here, out of all you PHP Freaks, know the answer? I will make it easy this time!I need the PHP command to download a file via FTP to the clients local drive. I thought it was ftp_fget or ftp_get, but they only save to the server side current web directory. That is assuming that I am doing it right. Here is my code:FTP connection already established and connection id -> $_SESSION['FTP'][code]function FTP_File ($dir,$file){ $remote_dir=$dir."/".$file; // Combining ftp dir and the slash and the filename $local_dir="c:\\".$file; if (!$handle = fopen($local_dir, 'w')) {echo "Cannot open file ($local_dir)"; exit;} fwrite($handle,$local_dir); echo "Getting file: $remote_dir, and putting it $local_dir<br>"; if(ftp_fget($_SESSION['FTP'],$handle,$remote_dir, FTP_ASCII, 0)){echo "SUCCESS!<br>";} else {echo "There was an error getting file from $remote_dir to $local_dir.<br>";} fclose($handle); }[/code] The result is the file gets saved as "c:\$file" on the web server's current directory, instead of going to a local "c:\" and saving it as $file.If you have an alternative solution PLEASE tell me! PLEASE!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/ Share on other sites More sharing options...
trq Posted August 17, 2006 Share Posted August 17, 2006 Right click-> Save as Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76304 Share on other sites More sharing options...
Jenk Posted August 17, 2006 Share Posted August 17, 2006 The reason you won't of had any 'help' from other places is because that operation is not possible.10quid you go elsewhere posting phpfreaks couldn't help :p Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76306 Share on other sites More sharing options...
kenrbnsn Posted August 17, 2006 Share Posted August 17, 2006 Actually, if there is an FTP server running on the client's local machine, using the FTP functions should be the way to go.You wrote: [quote]FTP connection already established and connection id -> $_SESSION['FTP'][/quote]Can you post how that connection was set up? This may have a clue to why your code isn't working.Ken Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76321 Share on other sites More sharing options...
-Mike- Posted August 17, 2006 Share Posted August 17, 2006 [quote author=akrytus link=topic=104643.msg417496#msg417496 date=1155830237]I need the PHP command to download a file via FTP to the clients local drive. I thought it was ftp_fget or ftp_get, but they only save to the server side current web directory. [/quote]Strange...http://uk.php.net/manual/en/function.ftp-get.phpThose indicate that it downloads to the current clients machine to me? Any verification? Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76322 Share on other sites More sharing options...
kenrbnsn Posted August 17, 2006 Share Posted August 17, 2006 You probably want to use the function ftp_put(), not ftp_get().Ken Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76326 Share on other sites More sharing options...
akrytus Posted August 17, 2006 Author Share Posted August 17, 2006 [quote]The reason you won't of had any 'help' from other places is because that operation is not possible.[/quote]So you are telling me the whole point of ftp_fget is to download files to the web server? That seems like such a waste. Why did they develop all these php commands just for a web server to send and recieve, but not the end user.[quote]Right click-> Save as[/quote]Cute, but no! I am creating a web based ftp inbedded in my site. So the ftp files and folders are not really there, they are being created by my website, so there is no right-click save as option. When clicked, my php connects to the FTP for you and downloads it for you. [quote]You probably want to use the function ftp_put(), not ftp_get().[/quote]That doesnt make sense, I want to get the file, not put it there.[quote]Those indicate that it downloads to the current clients machine to me? Any verification?[/quote]Thats what I thought. No errors, becuase it actually does download it, only to the current web dir. Example: [code] $file = test.txt$local_dir="c:\test.txt" $remote_dir="\public_ftp\test\test.txt"current web dir: \public_html\test2\[/code]The file is saved as: "\public_html\test2\c:\test.txt"The name of the file is [quote]"c:\test.txt"[/quote] not [quote]"test.txt"[/quote] in the c:\What am I doing wrong?Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76328 Share on other sites More sharing options...
akrytus Posted August 17, 2006 Author Share Posted August 17, 2006 [quote]Can you post how that connection was set up? This may have a clue to why your code isn't working.[/quote]FTP.php[code]<? // FTP$host="nocrs.net";$ftp_user_name="username";$ftp_user_pass="password";$hostip = gethostbyname($host);$conn_id = ftp_connect($hostip);// login with username and password$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);// IMPORTANT!!! turn passive mode onftp_pasv ( $conn_id, true );if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!"; echo "Attempted to connect to $host for user $ftp_user_name"; die;}else {}// Change directory to public_ftpif (ftp_chdir($conn_id, "public_ftp")) {}else { echo "Couldn't change directory!\n";}$_SESSION['FTP']=$conn_id; // Need to set global to work accross different functions?>[/code]index.php[code] require_once("../Connections/FTP.php"); // FTP connection and information[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76329 Share on other sites More sharing options...
akrytus Posted August 17, 2006 Author Share Posted August 17, 2006 Please dont give up on me! I am so close to finishing this, this is all I have left. I beg of you, FIND A WAY! Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76363 Share on other sites More sharing options...
kenrbnsn Posted August 17, 2006 Share Posted August 17, 2006 If I am understanding you correctly, you want to take a file that is currently on the web server and store it om the users' machine. If that is correct, you want to use ftp_put(). That is because your script is running on the Server.Ken Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76364 Share on other sites More sharing options...
akrytus Posted August 17, 2006 Author Share Posted August 17, 2006 [quote]If I am understanding you correctly, you want to take a file that is currently on the web server and store it om the users' machine. If that is correct, you want to use ftp_put(). That is because your script is running on the Server.[/quote]I like your thinking, why didnt I look at it like that? Out of curiosity, what if it is not on the webserver, could your write code to act on behalf of the client and have it sent to them? I let you know what I find out about that put. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76370 Share on other sites More sharing options...
akrytus Posted August 17, 2006 Author Share Posted August 17, 2006 Can you believe I get the exact same result? That didnt help, anything else? Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76373 Share on other sites More sharing options...
shoz Posted August 17, 2006 Share Posted August 17, 2006 If you want to send files to the user using PHP then use something similar to what is being used in the following topic.http://www.phpfreaks.com/forums/index.php/topic,104660.0.htmlThere are examples in the user contributed notes for [url=http://www.php.net/readfile()]readfile()[/url] and [url=http://www.php.net/header]header()[/url] that also demonstrate how to do this. There may be a tutorial on phpfreaks as well.If you need to go through an ftp then perhaps downloading the file with ftp_get, caching locally for future requests and then using the same method mentioned above would be an option. Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76420 Share on other sites More sharing options...
akrytus Posted August 18, 2006 Author Share Posted August 18, 2006 Ok, I have to read up on that, I am clueless, never used it. However I still have a question. I am assuming those command are good for files on the web server, if the files reside on a ftp server, same server just diferent directory outside of the public hmtl folder, can these still work? Or do I need to eliminate the ftp server and move the files to a folder in the public html folder? Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76900 Share on other sites More sharing options...
akrytus Posted August 18, 2006 Author Share Posted August 18, 2006 Thanks for the advice, I feel like I am much farther. However, after the file downloads, it is the correct file name, but the contents of the file is html. Infact it is the html code of the current visited page. Why? Here is my code:[code]if(isset($_SESSION['download'])){ $remote_file=$_SESSION['download']; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header('Content-length: '.filesize($file)); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=".basename($remote_file)); header( "Content-Description: File Transfer"); @readfile($remote_file); unset($_SESSION['download']);}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76937 Share on other sites More sharing options...
tomfmason Posted August 18, 2006 Share Posted August 18, 2006 I posted a script that uses this basic idea. Check it out at http://www.phpfreaks.com/forums/index.php/topic,104540.msg417030.html#msg417030Now the issue that I see with the code that you have posted is that I don't see the $file var defined anywhere. You should chage that from $file to $remote_file. Also, is the session download just the name of the download or the name and location. What I mean is say the file is whatever.zip. Is the session just whatever.zip or /the/path/to/whatever.zip. You can do something like this[code=php:0]if(isset($_SESSION['download'])){ $remote_file=$_SESSION['download']; $file = "path/to/downloads/$remote_file" header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header('Content-length: '.filesize($file)); header("Content-Type: application/force-download"); header( "Content-Disposition: attachment; filename=".basename($file)); header( "Content-Description: File Transfer"); @readfile($file); unset($_SESSION['download']);}[/code]Good luck,Tom Quote Link to comment https://forums.phpfreaks.com/topic/17859-you-php-freak-are-my-only-hope/#findComment-76989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.