PHP: How to read a .txt file from an FTP server into a variable?

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

I need to get the contents of a .txt file and put them in the $ variable. Here is my code that does not 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); ftp_get($conn_id, $content,"/" . $_POST["path"] . "/" . "#" . $result["id"] . " - " . $result["LastName"] . ", " . $result["FirstName"] . "/" . $_POST["title"] . ".txt", FTP_ASCII); 

This code connects to FTP, and I used ftp_get to copy from one text file to a variable called $ content. I know that the variable does not belong to this parameter, but I'm selling right now. I do not know how to read this .txt file.

Is there any way to do this using FTP FTP features?

thanks

Now when I try this:

 $fileContents = file_get_contents('ftp://username:paβ€Œβ€‹ ssword@hostname /path/to/file'); 

It gives a 550 error saying that the .txt file is not a regular file. Why am I getting this error?

Thanks again

Here is the official error I get:

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

+6
source share
3 answers

Try this, this code 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 an error, please change the file resolution to 777 or 775. I hope this helps.

+9
source

If you have already opened the connection via ftp_connect, this is probably the best answer:

 ob_start(); $result = ftp_get($conn_id, "php://output", $file, FTP_BINARY); $data = ob_get_contents(); ob_end_clean(); 
+4
source

file_get_contents is the easiest solution:

 $contents = file_get_contents('ftp://username:paβ€Œβ€‹ ssword@hostname /path/to/file'); 

If this does not work, this may be due to the fact that there are no URL handlers in PHP .


If you need more control over reading (transfer mode, passive mode, offset, read limit, etc.), use ftp_fget with the ftp_fget descriptor php://temp (or php://memory ) :

 $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)

+4
source

Source: https://habr.com/ru/post/952320/


All Articles