954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

infinite loads while writing a file with more than 10000 lines

Hello,
writing a file with more than 10000 lines, cause the page never ending loading. please help me to solve it.

this is the code i have used $store contains really 10000 lines

$fp = fopen($imgpath, 'w');
fwrite($fp, $store);
fclose($fp);

After this code i have redirect script, but that is not working. but redirect script works for 2000 lines and not more than that.

Regards
Chavan

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

It is possible that your script times out before reaching the end. You can try to use:

set_time_limit(0);

at the start of your script, to allow it to run (indefintely) until completed.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Hi pritaeas,

I have already used these in my program

ini_set('auto_detect_line_endings',1);
set_time_limit(0);
ini_set('post_max_size','250M');
error_reporting(0);

please any other solution.

Thanks for the reply

It is possible that your script times out before reaching the end. You can try to use:

set_time_limit(0);

at the start of your script, to allow it to run (indefintely) until completed.

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Did you try to change max_input_time and memory_limit ?

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Just wondering, why are you writing 10000 lines into a file?

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 
$fp = fopen($imgpath, 'w'); fwrite($fp, $store); fclose($fp);


Try replacing that code with the following:

file_put_contents($imgpath,$store);

A much more efficient function.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

cwarn23,

No that too did not help me. Please problem not solved yet.

Thanks for the reply

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Hello,

Anybody please help me in fixing this problem.

Regards
chavan

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Try this but you will need to wait for the page to be fully loaded before you expect to see results. So try the following:

<?
set_time_limit(0);
ini_set('memory_limit','200M');
//above are the very first lines of the document

//some code in here for $imgpath and $store

if (empty($imgpath)) {
    die('invalid image path');
    }
file_put_contents($imgpath,$store);
echo 'image stored at: '.$imgpath;
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

cwarn23,

I tried it but still could not solve the problem. please I'm trying with all soughts of different function but could not fix it. Please help me.

Thank you for the Reply

Regards
chavan

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

cwarn23,

I tried it but still could not solve the problem. please I'm trying with all soughts of different function but could not fix it. Please help me.

Thank you for the Reply

Regards chavan

perhaps post a little more of the code, the balls up could be the combination of another bottleneck with this one

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

Hi,
Any body please help me in fixing this issue. i'm really tried in trying in this issue for more than 5 days.

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
perhaps post a little more of the code, the balls up could be the combination of another bottleneck with this one

almostbob,

This is the complete code in the post of my form, please help me in solving this issue. the program is not getting redirected with large number of lines

here is the code you asked for
$list = file_get_contents($imgpath);
$listarray = array();
$listarray = explode("\n",$list);
$count_line = count($listarray);


$key = array();
$newline ='';
$store ='';
for($i =0; $i <= count($listarray) ; $i++)
{
if($i == count($listarray))
{

/*$fp = fopen($imgpath, 'w');
fwrite($fp, $store);
fclose($fp);*/
file_put_contents($imgpath,$store);
header("location: index.php");


}
$name = $listarray[$i];

// Replaces the Empty Line
$line = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", '', $name);
$line = trim($line);
if($line !='')
{
// Apply URL

$line = link($line);

$fault =0;
$key[] = $line;
$store .= trim($line)."\r\n";
}
}

Regards

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

I have checked your code and although it's a bit messy and buggy, a corrected version (still messy) is as follows:

$list = file_get_contents($imgpath);
$listarray = array();
$listarray = explode("\n",$list);
$count_line = count($listarray);


$key = array();
$newline ='';
$store ='';
for($i =0; $i <= count($listarray) ; $i++)
{
$name = $listarray[$i];

// Replaces the Empty Line
$line = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", '', $name);
$line = trim($line);
if($line !='')
{
// Apply URL

$line = link($line);

$fault =0;
$key[] = $line;
$store .= trim($line)."\r\n";
}
}
if (strlen($store)==0) {
die('<h1>Store is empty!</h1>');
}
/*$fp = fopen($imgpath, 'w');
fwrite($fp, $store);
fclose($fp);*/
file_put_contents($imgpath,$store);
header("location: index.php");
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

cwarn23,

I tried this before, it did not work, for that reason i have changed it into the new one , which i have posted already.

Thanks for the reply, Help me fix this, please

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Did you try my version anyway because I added 3 lines you wouldn't have had before and see if the page ends early with an error message telling you the problem.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Hi

Thank you, I forgot that three lines. But it doesn't work. what am I suppose to do

Regards

chavansoft
Newbie Poster
15 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

If the error saying "store is empty" did not trigger then I would suggest checking what those two variable contain. So run the following code to make sure that the variables are set correctly.

$list = file_get_contents($imgpath);
$listarray = array();
$listarray = explode("\n",$list);
$count_line = count($listarray);


$key = array();
$newline ='';
$store ='';
for($i =0; $i <= count($listarray) ; $i++)
{
$name = $listarray[$i];

// Replaces the Empty Line
$line = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", '', $name);
$line = trim($line);
if($line !='')
{
// Apply URL

$line = link($line);

$fault =0;
$key[] = $line;
$store .= trim($line)."\r\n";
}
}
die('<h1>$imgpath</h1>'.$imgpath.'<h1>$store</h1>'.$store);
/*$fp = fopen($imgpath, 'w');
fwrite($fp, $store);
fclose($fp);*/
file_put_contents($imgpath,$store);
header("location: index.php");

Also make sure $imgpath is a relative path and that the directories in it's path already exist.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You