fopen (): failed to open stream: permission denied in

0

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.
    
asked by JOMU 02.11.2017 в 13:51
source

2 answers

2

The problem

The problem lies in this line:

if (($handle = fopen($to_read,'r')) !== FALSE) {

Here the letter r tells fopen to open the file in mode r ead (read only), so, any attempt to write to the file will be denied.

The solution

Change the r mode to one of those allowed for writing according to the PHP Manual :

  • 'w' Open for write only ; Place the pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, it is tried to create.
  • 'w+' Opening for reading and writing ; Place the pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, it is tried to create.
  • 'a' Open for write only ; Place the pointer at the end of the file . If the file does not exist, you try to create it. In this mode, fseek() only affects the reading position; the readings are always postponed.
  • 'a+' Opening for reading and writing ; Place the pointer at the end of the file . If the file does not exist, you try to create it. In this mode, fseek() has no effect, the scripts are always postponed.
  • Etc ...
answered by 02.11.2017 в 23:44
1

For me it was useful to apply this sentence in linux setenforce 0

And with that you leave SElinux in permissive mode also apply all permissions chmod 777 to the directory where the file falls.

    
answered by 31.08.2018 в 19:22