在PHP中创建文件上载目录

时间:2014-09-16 10:02:24

标签: php session file-upload fileshare

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

问题出在以下几行:

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

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

fileshare.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>

upload.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();
              }


?>

1 个答案:

答案 0 :(得分:0)

您可能必须向用户提供&#34; www-data&#34;对/ home文件夹的权限。 如何做到这一点可以在这里找到: How to give apache permission to write to home directory?