Good morning. I'm doing a function in php to split a series of css files, located in an "x" directory, in other smaller files and when ordering execute the function I get the error "fopen (): failed to open stream: Permission denied in ... ".
I used this same function previously, processing one file at a time with the function and doing its job perfectly.
The problem comes when, using a foreach, I send the files from the "x" directory one by one to the function.
I have made sure that the folders have permissions 0777 changing these permissions by hand (I use appserv in local) and also from the script are given permissions with chmod to the folders involved.
I enclose the code of the function in case someone can guide me where the error is ...
function partir (){
$to_read = _PS_truncate_ . '/archivosUp' ; //Establecemos el directorio de lectura de archivos. Las partes se crearan en el directorio to_write.
$to_write = _PS_truncate_ . '/archivosUp/chunk' ; //Establecemos el directorio de escritura de archivos.
// Opciones No editables.
$done = false;
$part = 0;
if (($handle = fopen($to_read,'r')) !== FALSE) {
$header = fgets($handle);
while ($done == false) {
$locA = ftell($handle); // Obtiene la ubicación actual. COMIENZO
fseek($handle, $size, SEEK_CUR); // Saltar la longitud de $ size desde la posición actual
$tmp = fgets($handle); // Leer hasta el final de la línea. Queremos líneas completas
$locB = ftell($handle); // Obtiene la ubicación actual. FIN
$span = ($locB-$locA);
fseek($handle, $locA, SEEK_SET); // Saltar al inicio de este pedazo
$chunk = fread($handle,$span); // Lee, en modo binario seguro (fread), el trozo entre START y END
file_put_contents($to_write . '_' . $part.'.csv',$header.$chunk); //Escribe una cadena a un fichero (file_put_contents) formado como primer parametro por el fichero donde se escribe la información: archivo original ($to_read), guion bajo (_), la parte del csv en cuestión ($part), formato csv (.csv); como segundo parametro la información a escribir (data) encadenando $header y $chunk (lectura segura del archivo binario).
$part++;
if (strlen($chunk) < $size) $done = true; //Si la longitud del string (strlen) es menor que el tamaño indicado (Size) $done cambia a True y temina el while...
}
fclose($handle);
}
} //Cierra la función partir.