Jump to content

explode - fail.


shamuntoha

Recommended Posts

Trying to cut a large file in parts and print it , but why its now pringing? i am using phpDesigner 2008.

 

<?php
/**
* Large file devide in small part
* 
*/
//error_reporting(E_ALL);
// Take the whole file as string
$f1 = file_get_contents("log24");
// Reserve the size as len
$total = strlen($f1);

// Cut in pieces 
$part1  = substr($f1, 0, $total/2 );
//
$part2  = substr($f1, $total/2, $total);

// Part 1 - Array the needs
$ex1 = explode("---", $part1);

// Part 2 - Array the needs
$ex2 = explode("---", $part2);

echo $ex1[0];
echo $ex2[0];

  
exit(0);


?>

Link to comment
https://forums.phpfreaks.com/topic/130175-explode-fail/
Share on other sites

This is the error getting while trying outside phpDesigner 2008.

 

Fatal error: Allowed memory size of 103809024 bytes exhausted (tried to allocate 588 bytes) in D:\xampp\htdocs\jony\index.php on line 8

 

<?php
error_reporting(E_ALL);
ini_set("memory_limit","99M");
$f1 = file_get_contents("d:\\log24");
$total = strlen($f1);
//$deel1  = substr($f1, 0, $total/2 );
//$deel2  = substr($f1, $total/2, $total );
$ex = explode("---", $f1);
echo $ex[0];
exit(0);
?>

Link to comment
https://forums.phpfreaks.com/topic/130175-explode-fail/#findComment-675063
Share on other sites

Once i need to open it and split it by '---' to an array item. Output result is less, so its not risk for freezing the browser. But explode is the main requirement for this file.

???

log24 contain:

/*/**/*sadfdsf*adsfewe343242342343243324324adfdsfdsfdsfsdfdsfsdfsfsasfsfd --- /*/**/*sa3333333333333333333333333333342342343243324324adfdsfdsfdsfsdfdsfsdfsfsasfsfd --- /*/**/*sadfdsf*adsfewe343242342343243324324adfdsfdsfdsfsdfdsfsdfsfsasfsfd --- /*/**/*sadfdsf*adsfewe343244556566666666666fdsfdsfsdfdsfsdfsfsasfsfd ---

 

Link to comment
https://forums.phpfreaks.com/topic/130175-explode-fail/#findComment-675071
Share on other sites

Yes split in parts is also not helping showing the allocation error.

 

Fatal error: Allowed memory size of 83886080 bytes exhausted (tried to allocate 15358622 bytes) in D:\xampp\htdocs\jony\index.php on line 6

 

<?php
error_reporting(E_ALL);
ini_set("memory_limit","80M");
$f1 = file_get_contents("d:\\log24");
$total = strlen($f1);
$part1  = substr($f1, 0, $total/5 );
//$part 2= substr($f1, $total/2, $total );
$ex = explode("---", $part1);
echo $ex[0];
exit(0);
?>

Link to comment
https://forums.phpfreaks.com/topic/130175-explode-fail/#findComment-675075
Share on other sites

Did you mean to do following ? 3 different method try. new 2 as following.

 

Handle 1:

 

Notice: Undefined variable: line in D:\xampp\htdocs\jony\index.php on line 6

 

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 33292271 bytes) in D:\xampp\htdocs\jony\index.php on line 6

 

<?php
error_reporting(E_ALL);
//ini_set("memory_limit","M");
$file = fopen("d:\\log24", "r");
while ( !feof($file) ) { 	
$line = $line .  fgets($file); 
}
$ex = explode('---', $line); 
echo $ex[0];
exit(0);
?>

 

Handle 2:

 

Notice: Undefined variable: line in D:\xampp\htdocs\jony\index.php on line 10

 

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 31457281 bytes) in D:\xampp\htdocs\jony\index.php on line 10

<?php
error_reporting(E_ALL);
//ini_set("memory_limit","M");
$send = 0;
$chunks = (2 << 20);
$total = filesize("d:\\log24");
$file = fopen("d:\\log24", "r");

while ( $send < $total ) { 	
$line = $line . fread($file, $chunks);
$send += $chunks; 
}
$ex = explode('---', $line); 
echo $ex[0];
exit(0);
?>

 

Link to comment
https://forums.phpfreaks.com/topic/130175-explode-fail/#findComment-675083
Share on other sites

Thanks, i came to this code, which is much faster.

 

<?php

/**

* If any errors

* - Large file handle

* - 100mb to 200mb

* - unix grep use, tail etc

*/

error_reporting(E_ALL);

// File total size

$fsize = filesize("d:\\log24");

 

// for ini_set $fsize += 32 * 100 * 1024;

// ini_set("memory_limit", $fsize . "M");

// Set memory to use

ini_set("memory_limit","90M");

 

// Start and Download move

$send = 0;

// Set the chunk

$chunk = (2 << 20);

// Create large array.

$buffer = array();

// File resource

$ex = fopen("d:\\log24", "rb");

// Attempts to try

$i=0;

// Loop to get data as binary safe

while($send <= $fsize){

$buffer[$i] = explode('---', fread($ex,$chunk) );

$send += $chunk;

$i++;

}

// Close resource

fclose($ex);

// Clear send

unset ($send);

 

// Output the data

foreach($buffer as $key=>$value){

echo $key;

}

// Clear ram

unset($buffer);

 

// Exit

exit(0);

 

?>

Link to comment
https://forums.phpfreaks.com/topic/130175-explode-fail/#findComment-675165
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.