Jump to content

Archived

This topic is now archived and is closed to further replies.

akrytus

You, PHP Freak, are my ONLY hope!

Recommended Posts

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!!!!!!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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.php

Those indicate that it downloads to the current clients machine to me? Any verification?

Share this post


Link to post
Share on other sites
You probably want to use the function ftp_put(), not ftp_get().

Ken

Share this post


Link to post
Share on other sites
[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?

Share this post


Link to post
Share on other sites
[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 on
ftp_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_ftp
if (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]


Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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!

Share this post


Link to post
Share on other sites
Can you believe I get the exact same result?  That didnt help, anything else?

Share this post


Link to post
Share on other sites
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.html

There 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
I posted a script that uses this basic idea. Check it out at http://www.phpfreaks.com/forums/index.php/topic,104540.msg417030.html#msg417030

Now 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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.