Jump to content

[SOLVED] Opening file, getting first two lines, modifiying file


Recommended Posts

I would check out this page: http://us.php.net/file

<?php
// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}

// Another example, let's get a web page into a string.  See also file_get_contents().
$html = implode('', file('http://www.example.com/'));

// Using the optional flags parameter since PHP 5
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>

 

Notice that this will only work if your server supports opening remote files with file.

 

Next, I would check out this article: http://forums.devshed.com/ftp-help-113/using-php-ftp-to-edit-a-file-on-a-remote-179877.html

 

<?php

$config = array(
    'ftp_user'  => '*****',
    'ftp_pass'  => '*****',
    'domain'    => 'example.com',
    'file'      => 'file.txt',       # relative to 'domain'
);

$fp = fopen($config['file'],'w');
fwrite($fp,stripslashes($_POST['newd']));
fclose($fp);

$ftp = ftp_connect($config['domain']);
ftp_login($ftp,$config['user'],$config['pass']);
ftp_put($ftp,$config['file'],$config['file'],FTP_ASCII);
ftp_close($ftp);
?>

Then how should I do it if I want the get the first line into a variable, and the second line into another variable, and so on?

 

This is what arrays are for, and this line....

 

$lines = file('http://www.example.com/');

 

Does exactly that. With each line going into an index within the $lines array.

I see. What if I just want to strip the first two lines? Would I use the existing query, and then just start with $lines[3] and on? If so, how would I make it go on forever, or at least until there are no more lines?

All you need do is setup a counter prior to your loop, then increment it as you iterate through the contents of the file. The first two iterations you would not save the content. eg;

 

$i = 0;
foreach (file('file.txt') as $line) {
  if ($i > 1) {
    $contents .= $line;
  }
  $i++;
}

 

Now you can store $contents into your file.

I don't really understand your question.  But if you are just trying to remove the first two lines:

<?php

$filename = 'file.txt';

// open remote file
$file_contents = file("http://www.example.com/$filename");

// remove first two lines
$file_contents = array_slice($file_contents, 2);

// rejoin lines in to one big file
$file_contents = implode('', $file_contents);

// ftp connection info
$ftp_user = '*****';
$ftp_pass = '*****';
$ftp_domain = 'example.com';

// connect to ftp, get ftp connection handle
$ftp = ftp_connect($ftp_domain);

// login to ftp
ftp_login($ftp, $ftp_user, $ftp_pass);

// write file to ftp
ftp_put($ftp, $filename, $file_contents, FTP_ASCII);

// close ftp connection
ftp_close($ftp);
?>

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.