waiwai933 Posted December 16, 2008 Share Posted December 16, 2008 How do I open a file using HTTP in PHP, of course, and get just the first two lines. This would be a txt file. Then, how would I open the same file in FTP, and delete the first two lines? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/ Share on other sites More sharing options...
flyhoney Posted December 16, 2008 Share Posted December 16, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716309 Share on other sites More sharing options...
waiwai933 Posted December 16, 2008 Author Share Posted December 16, 2008 Thank you so much. I just have a question regarding the first part. How do I get it so that it only takes X number of lines? Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716368 Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 You don't. But you can skip the first few iterations. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716371 Share on other sites More sharing options...
waiwai933 Posted December 16, 2008 Author Share Posted December 16, 2008 So I can't get it to just take the first two lines? 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? Preferably without the so on. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716375 Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716378 Share on other sites More sharing options...
waiwai933 Posted December 16, 2008 Author Share Posted December 16, 2008 I see, sort of. How would I call a specific number of an array? Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716380 Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 $lines[0] is the first line, $lines[1] the second etc etc. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716381 Share on other sites More sharing options...
waiwai933 Posted December 16, 2008 Author Share Posted December 16, 2008 Ok, thank you. That's that headache. Now, for the second piece of code, is it possible to strip just the first two lines instead of the whole thing? And what does the $_POST['newd'] supposed to be? Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716405 Share on other sites More sharing options...
waiwai933 Posted December 16, 2008 Author Share Posted December 16, 2008 Please help? Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716416 Share on other sites More sharing options...
flyhoney Posted December 16, 2008 Share Posted December 16, 2008 In that example, $_POST['newd'] is just whatever you want to write to the file. Change that out with whatever you want. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-716697 Share on other sites More sharing options...
waiwai933 Posted December 17, 2008 Author Share Posted December 17, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-717231 Share on other sites More sharing options...
waiwai933 Posted December 17, 2008 Author Share Posted December 17, 2008 Could someone please help? I've tried to use array_shift, but that doesn't seem to work. Maybe I'm using it wrong? Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-718307 Share on other sites More sharing options...
trq Posted December 17, 2008 Share Posted December 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-718323 Share on other sites More sharing options...
flyhoney Posted December 17, 2008 Share Posted December 17, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-718332 Share on other sites More sharing options...
waiwai933 Posted December 17, 2008 Author Share Posted December 17, 2008 Thank you to both of you so, so much. Quote Link to comment https://forums.phpfreaks.com/topic/137131-solved-opening-file-getting-first-two-lines-modifiying-file/#findComment-718338 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.