0

我正在创建一个文件上传表单,要求在 file_share.php 中输入用户名,然后应该转到允许用户上传文件的上传表单 upload.php。

问题出在这条线上:

`$full_path = sprintf("/home/dfatoki11/uploading/%s/%s", $username, $filename);`

当我运行该文件时,它说无法写入会话数据并且错误是权限被拒绝。设置临时文件以存储上传的文件时,我使用哪个目录以及如何在 aws/apache Web 服务器上设置权限?

文件共享.php

<?php
session_start();
  $username = $_SESSION['username'];
  echo $_SESSION['username'];
  ?>

<?php
$myFiles = sprintf("/home/dfatoki11/uploading/%s/%s", $userName, $filename);
$files=scandir($myFiles);

$full_path = sprintf("/home/dfatoki11/uploading/%s/%s", $username, $filename);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($full_path);

header("content-Type:" .$mime);
readfile($full_path);
?>

<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>
<label for="file">Choose a file to upload:</label> 
<input name="uploadedfile" type="file" id="file" />
<input type="submit" value="Upload File" />
</p>
</form>

上传.php

<?php
        session_start();
        // Get the filename and make sure it is valid
        $filename = basename($_FILES['uploadedfile']['name']);
        if( !preg_match('/^[\w_\.\-]+$/', $filename) ){
           echo "Invalid filename";
           exit();
        } 
        // Get the username and make sure it is valid
        $username = $_SESSION['username'];
        if( !preg_match('/^[\w_\-]+$/', $username) ){
           echo "Invalid username";
           exit();
        } 
        $full_path = sprintf("/home/dfatoki11/uploading/%s/%s", $username, $filename);

              if( move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $full_path) ){
                 header("Location: file_share.php");
                 exit();
              }else{
                 header("Location: login.php");
                 exit();
              }


?>
4

1 回答 1

0

您可能必须授予用户“www-data”对您的 /home 文件夹的权限。如何做到这一点可以在这里找到: How to give apache permission to write to home directory?

于 2014-09-16T10:38:08.437 回答