0

So I've made a FTP server, and I'm able to upload files to it. But now I want to display a .log files content (plain text), but I can't seem to get how this should be done.

I've made a class with this function, and I can see that the uploaded file exists, but how do I print the content of 4404.log?

function FTP_read(){
    ob_end_flush();
    $conn_id = ftp_connect($this->ftp_server);

    $login_result = ftp_login($conn_id, $this->ftp_user_name, $this->ftp_user_pass);
    // get contents of the current directory
    $contents = ftp_nlist($conn_id, ".");

    // output $contents
    var_dump($contents);
}

enter image description here

I thought that I could use ftp_get(), but why do I have to write to another file?

ftp_get() retrieves a remote file from the FTP server, and saves it into a local file.

Expected to get something like this (print log content):

enter image description here

  • To your Question about why do you have to write. This is how FTP Works, if you use Filezilla for example it creates a temporary file and write the content into the file in order to read it. you could do similar things. Create a temporary file with tmpfile() then download the content and write into it and then outputs everything and delete the file – Vitalij Mik Dec 21 '21 at 14:34
  • @VitalijMik + CBroe – None of that means that you have to download the contents to a physical local file, if you do not need that. It's perfectly possible to use FTP download the remote file contents to local memory only. – Martin Prikryl Dec 21 '21 at 14:36
  • @MartinPrikryl Yes this works, but is it possible to read it line by line, instead of dumping it all in one line? – Mads Sander Høgstrup Dec 21 '21 at 14:41
  • It does not *"dump it all in one line"*. Anyway, what do you aim for? Are you just asking how to split the contents to individual lines? Or are you asking because the contents is to large to fit into memory, so you have to process it by lines? You question asks how to print the contents. If you have more specific needs, you should edit your question accordingly. – Martin Prikryl Dec 21 '21 at 14:47
  • @MartinPrikryl Right now the files isn't too large, but I cant tell if they will be in the feature. But each line in the log contains different information, like every 10th line could be `DEBUG...`, so I want the file line by line, so I have more control later on when I'm gonna create some JS line filtering on the logs, so it's possible to hide all `DEBUG...` lines from the text etc.. – Mads Sander Høgstrup Dec 21 '21 at 14:52
  • @MadsSanderHøgstrup see this Answer in first comment https://stackoverflow.com/a/18393280/7468974 in the fread do not pass here the filesize but 1024 for example – Vitalij Mik Dec 21 '21 at 15:01
  • @MartinPrikryl so by looking at your link and with some testing i made something work, which I posted as a answer. Now I'm able to print the log files line by line, is this what you had in mind? – Mads Sander Høgstrup Dec 21 '21 at 15:07

1 Answers1

0

Created a connection, and opened a php://temp file, then used ftp_fget(...) to write to that file. Then the $h file had the content, so I just had to loop through that file line by line, which seems to work.

function FTP_read(){
    ob_end_flush();
    $conn_id = ftp_connect($this->ftp_server);

    $login_result = ftp_login($conn_id, $this->ftp_user_name, $this->ftp_user_pass);

    ftp_pasv($conn_id, true);

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

    ftp_fget($conn_id, $h, '4404.log', FTP_BINARY, 0);

    fseek($h, 0);

    while(!feof($h)) {
        $line = fgets($h);
        echo "$line"."<br>";
    }

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