警告:move_uploaded_file(**路径**):无法打开流:

时间:2016-03-31 12:10:06

标签: php file-upload

嘿,我总是得到错误

  

警告:move_uploaded_file(路径):无法打开流:   第27行/Users/Shared/xampp-htdocs/php-reports/upload.php中的权限被拒绝

我的index.php代码是

<!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>

我的php代码是

<?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>';
?>

我还将目录设置为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

1 个答案:

答案 0 :(得分:1)

首先你应该检查谁是apache用户因为chmod不够尝试在终端$ ps aux |中执行此操作egrep'lampp'然后你将获得apache用户,这是唯一有权将文件上传到该文件夹​​的用户,然后通过键入chown -R“将你的apache用户放在我的cas中是守护进程”,将该foder的所有者更改为该用户“/ Users / Shared / xampp-htdocs / php-reports /

相关问题