我正在创建一个文件上传表单,要求在 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();
}
?>