简体   繁体   中英

Warning: move_uploaded_file(**Path**): failed to open stream: Permission denied in

Hey i alaways get the error

Warning: move_uploaded_file( Path ): failed to open stream: Permission denied in /Users/Shared/xampp-htdocs/php-reports/upload.php on line 27

my index.php code is

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
      <input type="file" name="datei" accept=".csv"><br>
      <input type="submit" value="Hochladen">
    </form>
  </body>
</html>

and my php code is

<?php
$upload_folder = getcwd().'/uploads/files'; //Das Upload-Verzeichnis
$filename = pathinfo($_FILES['datei']['name'], PATHINFO_FILENAME);
$extension = strtolower(pathinfo($_FILES['datei']['name'], PATHINFO_EXTENSION));


//Überprüfung der Dateiendung
$allowed_extensions = array('csv');
if(!in_array($extension, $allowed_extensions)) {
  die("Ungültige Dateiendung. Nur csv-Dateien sind erlaubt");
}


//Pfad zum Upload
$new_path = $upload_folder.$filename.'.'.$extension;

//Neuer Dateiname falls die Datei bereits existiert
if(file_exists($new_path)) { //Falls Datei existiert, hänge eine Zahl an den Dateinamen
  $id = 1;
  do {
    $new_path = $upload_folder.$filename.'_'.$id.'.'.$extension;
    $id++;
  } while(file_exists($new_path));
}

//Alles okay, verschiebe Datei an neuen Pfad
move_uploaded_file($_FILES['datei']['tmp_name'], $new_path);
echo 'Datei erfolgreich hochgeladen: <a href="'.$new_path.'">'.$new_path.'</a>';
?>

i also set the directory to chmod 777

-rw-r--r--  1 user  staff   279 31 Mär 12:00 .htaccess
drwxrwxrwx  2 user  staff    68 31 Mär 11:56 files

first you should check who is the apache user because chmod is not sufficient try to do this in terminal $ ps aux | egrep 'lampp' then you will get the apache user which is the only user having right to upload files into that folder and then change the owner of that foder to this user by typing chown -R "your apache user here in my cas is daemon" /Users/Shared/xampp-htdocs/php-reports/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM