Jump to content

FTP-PHP show the rows from line x to y


CaTaLinU

Recommended Posts

You cannot do this with the ftp commands (except possibly by using file manipulation commands like fopen/fseek/fread through ftp_raw, if permissions allow, which most likely not..also, I'm not entirely sure you can actually use those commands).  You will have to retrieve the file as a whole, and then search through it.  You may (with proper permissions local and remotely) be able to do it with sockets though with limitations. (Sockets would in theory allow you to run arbitrary shell commands on the remote server, so for instance you could run php remotely and use the file reading functions, or use shell commands).    Either way...

 

None of the file reading commands (whether done locally or through a socket  allows you to just grab x through y lines unless the lines are by convention all a fixed number of char length (padded if shorter), and then you can use fseek to jump to the line based on $x * $length. If the lines are not fixed length, you will have to either make a loop and just continue until $x (and then break at line $y), or put the file in its entirety into an array (like with file - not advisable unless the filesize is always expected to be relatively small). 

 

edit: nevermind looks like you actually can't even use ftp_exec or ftp_raw.  ftp_exec only returns a status code or false, no info, so even if the remote server allowed it, it would be useless.  ftp_raw only (assuming permissions allow for it) only do raw ftp commands, not php commands!

Link to comment
Share on other sites

I have done this code , and it works perfectly with little bugs ...

 

if i set to read the lines from 0 to 20 , on the page it shows that lines

 

but if i set to show the lines from 20 to 40 , it shows the lines from 0 to 20 on page ... that is the bug or the error...

or if i set to 40 to 80 it shows the lines from 0 to 40 because

80 - 40 = 40

40 - 20 = 20 ....

 

$handle = fopen("file.txt", "r");
$lenght_parameter = "9999999";
$ic = 20;
$ic_max = 40;  // stops after this number of rows
   while (!feof($handle) && ++$ic<=$ic_max) {
        $buffer = fgets($handle, $lenght_parameter);
        echo $buffer."<br>";
    }
fclose($handle);

Link to comment
Share on other sites

You need to skip the first lines for that to work.  That means you need a different variable to remember which line you are on, AND the start line and end line.  You would skip lines while $i <= $ic, then print lines while $i <= $ic_max.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.