$input = explode("\n", file_get_contents("file.csv"));
foreach ($input as $line) {
// process all lines.
}
// This function removes first 100 elements.
// More info:
// http://php.net/manual/en/function.array-slice.php
$output = array_slice($input, 100);
file_put_contents("out.csv", implode("\n", $output));
The problem is happened when I trying to run the above code and I get:
"failed to open stream permission denied"
You have 3 cases here where you can't modify the file.
- File Already in Use (Locked by another process).
- File does not have the right permissions (R/W). Or file does not belong to the user/group you are using, in must of php cases it's (www-data).
- File does not exist.
1) If the file is already in use, you can't modify it, but you can try something else *
A RACE CONDITION *
that can make it happen :
# RACE CONDITION LOOP
# The only way you have here is to setup a RC loop to check if/when the file can be written.
$Path = dirname ( __FILE__ ) . "path_to_your_file/out.csv";
$tries = 99; // Any number of tries you want.
for ( $i = 1; $i < $tries; $i++ )
{
$output = array_slice ( $input, 100 );
{
if ( @file_put_contents ( $Path , implode ( "\n", $output ) ) )
{
echo "Content Added!"."<br>";
$i = $tries;
}
else
{
echo "Content Cannot be Added!"."<br>";
}
}
ob_flush ( );
echo "We Have tried " . $i . " times to add content to the file"."<br>";
sleep ( 1 );
flush ( );
}
2) If File does not have the right permissions you can set it in your code :
$Path = dirname ( __FILE__ ) . "path_to_your_file/out.csv";
$Mode = 0755; // permissions wanted
$Umsk = @umask( 0 );
@chmod ( $Path, $Mode );
@umask ( $Umsk );
3) If File does not exist you can create it, but first make sure to check if it's there :
# Make sure the file that contain this code have the right permissions, user/group .
# If not this cannot create the file and will not allow you execute the whole code .
$Path = dirname ( __FILE__ ) . "path_to_your_file/out.csv";
$Mode = 0755; // permissions wanted
$Check_File = file_exists ( $Path );
if ( $Check_File ) { $File_Exist = true; }
else { $File_Exist = false; }
if ( $File_Exist )
{
@touch ( $Path );
@chmod ( $Path, $Mode );
}
else
{
echo "File Already Exist";
}
I hope this can help and if not please specify what exactly the problem is.
PS : for memory limit errors some servers and/or host provider does not allow this in user's php.ini or from php to be set. you must edit the global config files for both php and apache, and maybe mysql too in case you are using big amount of data for database, to allow a max chosen value for memory limit .