Permissions to files uploaded with PHP

0

This is my subcode of the archo.

if(empty($error)){
        $archivo = "/".$dni."_".$nombreArchivo;
        $ruta = $directorioSubida.$archivo;
        $consul=$conn->prepare("SELECT * FROM archivos where guardar = ?");
        $consul->bindParam(1,$archivo);
        $consul->execute();
        $num = $consul->rowCount();
        if($num>0){
            $ruta = $directorioSubida."/(".$num.")".$dni."_".$nombreArchivo;
        }

    if(move_uploaded_file($_FILES['archivo']['tmp_name'], $ruta)){

        $consult= $conn->prepare("INSERT into archivos values(null,?,?,?,?)");
        $consult->bindParam(1,$nombre);
        $consult->bindParam(2,$ruta);
        $consult->bindParam(3,$archivo);
        $consult->bindParam(4,$dni);
        $consult->execute();
        if($consult){
            //header("Location: subidaarchivos.php");
            chmod($ruta,0777);
            echo substr(sprintf("%o",fileperms($ruta)),-4);

        }

    }

}

This is my file deletion element uploaded

  if(isset($_REQUEST['eliminararchivo'])){
    $id = $_POST['id'];
    $eliminar = $conn->prepare("DELETE from archivos where idarchivo = ?");
    $eliminar->bindParam(1,$id);
    $eliminar->execute();
    if($eliminar){
        $select = $conn->prepare("SELECT * FROM archivos where idarchivo = ?");
        $select->bindParam(1,$id);
        $select->execute();

        $fila1 = $select->fetch();
        $ruta = $fila1[2];
        $numero = strlen($ruta);
        $ruta = substr($ruta, 1,$numero);
        $ruta = "..".$ruta;
        unlink($ruta);
    }
  }

The problem is that when you delete with php with unlink it puts me: " unlink (..) : Permission denied" You know the reason or some solution. Thank you very much.

    
asked by bsg 18.05.2017 в 18:14
source

1 answer

1

What you're looking for is the chmod method. You could give them the permissions once the file was uploaded as follows:

move_uploaded_file($_FILES['archivo']['tmp_name'], $ruta);
chmod($ruta, 0666);

This method has to be executed once it has been uploaded, and before trying to delete it with unlink .

    
answered by 18.05.2017 / 18:31
source