8

I have two servers. I already have a .txt file in the one I'm connecting to.

I need to get the .txt file contents and put them into a $ variable. Here is my code that doesn't work:

$ftp_server = $_POST["site"];
$path = $_POST["path"];
$ftp_user_name = $_POST["username"];
$ftp_user_pass = $_POST["pass"];

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

$remote_filename =
    "/" . $_POST["path"] . "/" . "#" . $result["id"]
    . " - " .    $result["LastName"] . ", " . $result["FirstName"]
    . "/" . $_POST["title"] . ".txt";
ftp_get($conn_id, $content, $remote_filename, FTP_ASCII);

This code connects to the FTP and I used the ftp_get to copy from one text file to a variable called $content. I know a variable doesn't belong in this parameter, but I'm sold right now. I have no idea how to read this .txt file.

Is there a way to do this with the PHP FTP functions?

Now, when I try this:

$fileContents = file_get_contents('ftp://username:pa‌​ssword@hostname/path/to/file');

It gives an error:

Warning: fopen(ftp://...@goldenbooklet.com/101Notebook Backup: 22-08-2013/#11 - Cappucci, Ryan/queen.txt) [function.fopen]: failed to open stream: FTP server reports 550 /101Notebook Backup: 22-08-2013/: not a regular file in /home/content/34/11614434/html/adminpdo.php on line 269

Why am I getting this error?

Thanks again

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

3 Answers3

19

The file_get_contents is the easiest solution:

$contents = file_get_contents('ftp://username:pa‌​ssword@hostname/path/to/file');

If it does not work, it's probably because you do not have URL wrappers enabled in PHP.


If you need greater control over the reading (transfer mode, passive mode, offset, reading limit, etc), use the ftp_fget with a handle to the php://temp (or the php://memory) stream:

$conn_id = ftp_connect('hostname');

ftp_login($conn_id, 'username', 'password');

ftp_pasv($conn_id, true);

$h = fopen('php://temp', 'r+');

ftp_fget($conn_id, $h, '/path/to/file', FTP_BINARY, 0);

$fstats = fstat($h);
fseek($h, 0);
$contents = fread($h, $fstats['size']); 

fclose($h);
ftp_close($conn_id);

(add error handling)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
12

If you've already opened a connection via ftp_connect, this would probably be the best answer:

ob_start();
$result = ftp_get($conn_id, "php://output", $file, FTP_BINARY);
$data = ob_get_contents();
ob_end_clean();
tobidude
  • 480
  • 1
  • 7
  • 11
9

Try this, this code is from http://www.php.net/manual/en/function.fread.php

<?
$filename = "ftp://username:pa‌​ssword@hostname/path/to/file";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

echo $contents;
?>

If error, please change the file permission to 777 or 775. Hope this may help.

  • I still get an error when I try that. I posted the error above. – Ryan Jeffrey Cappucci Aug 23 '13 at 01:40
  • The code above is work fine with ordinary .txt file. Is it possible that the file you going to read is not a file (according from your link, it's look like you're trying to read a folder) or may be it's .php or .aspx file? If so you can not get that .php file. – Palm Underscorez Aug 23 '13 at 01:59
  • Yes it is a txt file, but for some reason no matter what I try I still get the error. – Ryan Jeffrey Cappucci Aug 23 '13 at 02:41
  • please read this article >> http://support.ipswitch.com/kb/WS-20000817-DM02.htm it's about error 550. May be it's not coding problem. – Palm Underscorez Aug 23 '13 at 02:47
  • You're getting this error because you're trying to read from a stream, and reading stops when a packet is available. Note that data streams are sent in packets. To do this, you need the following: ` $filename = "ftp://username:pa‌​ssword@hostname/path/to/file"; $handle = fopen($filename, "rb"); $contents = stream_get_contents($handle); fclose($handle); echo $contents; ?>` – Terence Feb 26 '18 at 08:04