Split file every n lines [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I’m working with the royal mail PAF database in csv format (approx 29 million lines), and need to split the data into files of approx 1000 lines.
I found this solution to write the files, but do not know how to a. open the file, and b. tell the script to delete the lines from the original file after copying them.
Can anyone advise?

Does it need to be in PHP? If you're on a Unix/Linux system, you can use the split command.
split --lines=1000 mybigfile.csv
http://en.wikipedia.org/wiki/Split_%28Unix%29

I don't know the royal PAF database, but you open files with fopen(), read a line with fgets() and delete files with unlink().
Your found solution shows the idea of splitting every 1000 lines, but in your case there is no need for calling any csv function at all. It's just a simple "copy each 1000 lines into new file".
$bigFile = fopen("paf.csv", "r");
$j = 0;
while(! feof($bigFile)) {
$smallFile = fopen("small$j.csv", "w");
$j++;
for ($i = 0; $i < 1000 && ! feof($bigFile); $i++) {
fwrite($smallFile, fgets($bigFile));
}
fclose($smallFile);
}
fclose($bigFile);
unlink("paf.csv");

Related

PHP do-while loop filesize check and stop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So the problem is I'm having is looping to check the file size, for example a file A.txt is 10 bytes and with other php scripts A.txt changes to 20bytes so when the A.txt file is changed I want the iteration to stop.
<?php
$i = 1;
$a = filesize("A.txt");
do {
$c = filesize("A.txt");
if ($a >= $c) {
} else {
echo file_get_contents('chat.log');
$i++;
}
} while ($i <= 2);
?>
What is my mistake?
It seems like you want to implement long-polling for a chat application.
This approach could lead you to lose some chat messages being delivered. And this is not very scalable.
I would suggest you to receive the previously known file size via a GET parameter, and check if the file size is larger than that.
Then I would suggest you to use fopen to open the file and read from the previous pointer (GET parameter) to the end of file, than reading the whole file every time.
you may also want to clearstatcache before each filesize, and add a usleep(100); inside the while loop.

File Writing in php without appending [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Following is my piece of code which writes to file.
<?php
$fileWrite = fopen("c.txt", "w+");
for($i=0;$i<5;$i++) {
$bytes = fwrite($fileWrite, $i);
}
fclose($fileWrite);
I am getting 01234. It means , pointer is appending to last location, I don't want to append data. Instead need to write 4 in the file.
Then you need to ftruncate the file before writing to it:
ftruncate($fileWrite, 0);
$bytes = fwrite($fileWrite, $i);
This is obviously pretty pointless to do in a loop, but I expect you know that.
I personally recommend using file_put_contents for this simple task. It's easier to use and will not append to the end of the file (unless specified that way).

Adding to .txt file in PHP instead of overwriting it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this line of PHP code here
<?php
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
?>
Whenever
phpfile.php?blablabla
is queried it writes a query.txt with the parameter in it, in this case, blablabla.
BUT, when I do this, it deletes the past query.txt file and writes a totally new one.
I want the queries to be ADDED into the .txt file. So there can be as many queries and possible and every single value entered will be lined up in the .txt file..
For example
I want it so phpfile.php?test is visited, query.txt looks like this
test
after that, phpfile.php?test2 is visited
now query.txt looks like this
test
test2
and this goes on forever.
How do I do this?
p.s.: sorry, I'm totally a Java type. I'm an absolute beginner to PHP.
You wish to append to the file, so you should use something along the lines of
$handle = fopen("myfile.txt", "a");
fwrite ($handle, parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
fclose($handle);
You can find all the other read/write file open flags at the docs.
http://php.net/manual/en/function.fopen.php
It appears you can also just pass a flag with your existing code, as that will abstract the file handle open/close from you;
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), FILE_APPEND);
Add FILE_APPEND as 3rd argument to file_put_contents(). Example (.PHP_EOL to add new line):
$data = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY).PHP_EOL;
file_put_contents('query.txt', $data, FILE_APPEND);

PHPWord bold certain words on a line [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering if there was a way to bold certain words on a line. For example if I wanted every third word on a line bold, how would I do it. I am currently using addText but that requires the whole line to be bold or not bold. Any responses would be greatly appreciated.
You will need to use createTextRun() method. I have tried with Text.php file from Examples folder, and here is code relevant for your problem:
$textrun = $section->createTextRun();
$sentence='I am sentence, and every third word will be bold. This is bold.';
$word_arr=explode(' ', $sentence);
$styleFont = array('bold'=>true, 'size'=>16, 'name'=>'Calibri');
$styleFont2 = array('bold'=>false, 'size'=>16, 'name'=>'Calibri');
$c = 0;
for($i = 0; $i < count($word_arr); $i++)
{
$c++;
if($c % 3 == 0)
{
$textrun->addText($word_arr[$i].' ', $styleFont);
}
else
{
$textrun->addText($word_arr[$i].' ', $styleFont2);
}
}
You could tweak it to get what you want, but, basically, by using of mentioned method, it is possible to get different styles in same line.

how to put contents of a zip file in to an array using php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a zip file and I want to put contents of it in an array , actually names or directory of contents .
I have some ideas using ZipArchive but . . .
is there any simple idea ? :)
You don't need any complicated ideas:
$zip = new ZipArchive;
$zipList = array();
if ($zip->open('myZipFile.zip') === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$zipList[] = $zip->getNameIndex($i);
}
} else {
echo 'Error reading zip archive';
}
is that simple enough?
EDIT
A simple reading of the documentation would offer you:
file_put_contents($zip->getNameIndex($i), $zip->getFromIndex($i));
as long as the directories exist
or use the extractTo() method
This is pretty well documented in the manual, and there's plenty of examples in the comments; you really shouldn't need to ask on SO for something that is documented as well as this

Categories