Jump to content


Photo

You, PHP Freak, are my ONLY hope!


  • Please log in to reply
15 replies to this topic

#1 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 03:57 PM

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']

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);	
}


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

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 17 August 2006 - 03:58 PM

Right click-> Save as

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 04:03 PM

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

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 August 2006 - 04:15 PM

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:

FTP connection already established and connection id -> $_SESSION['FTP']


Can you post how that connection was set up? This may have a clue to why your code isn't working.

Ken

#5 -Mike-

-Mike-
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 17 August 2006 - 04:16 PM

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. 


Strange...

http://uk.php.net/ma...ion.ftp-get.php

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


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 August 2006 - 04:22 PM

You probably want to use the function ftp_put(), not ftp_get().

Ken

#7 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 04:30 PM

The reason you won't of had any 'help' from other places is because that operation is not possible.


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.


Right click-> Save as


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. 

You probably want to use the function ftp_put(), not ftp_get().


That doesnt make sense, I want to get the file, not put it there.


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


Thats what I thought.  No errors, becuase it actually does download it, only to the current web dir. 

Example:
$file = test.txt
$local_dir="c:\test.txt" 
$remote_dir="\public_ftp\test\test.txt"

current web dir: \public_html\test2\
The file is saved as:

"\public_html\test2\c:\test.txt"

The name of the file is

"c:\test.txt"

not

"test.txt"

in the c:\

What am I doing wrong?


Any ideas?

#8 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 04:35 PM

Can you post how that connection was set up? This may have a clue to why your code isn't working.


FTP.php
<?  // 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
?>

index.php
  require_once("../Connections/FTP.php");  // FTP connection and information




#9 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 06:04 PM

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!

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 August 2006 - 06:09 PM

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

#11 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 06:25 PM

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.


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!

#12 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 17 August 2006 - 06:37 PM

Can you believe I get the exact same result?  That didnt help, anything else?

#13 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 17 August 2006 - 08:21 PM

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...c,104660.0.html

There are examples in the user contributed notes for readfile() and header() 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.

#14 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 18 August 2006 - 07:13 PM

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?

#15 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 18 August 2006 - 08:01 PM

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:

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']);}


#16 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 18 August 2006 - 09:01 PM

I posted a script that uses this basic idea. Check it out at http://www.phpfreaks....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

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']);}

Good luck,
Tom


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users