move_uploaded_file上传功能在 AWS EC2 实例中失败,并显示消息"failed to open stream: Permission denied"



我有一个最简单的php页面,用户只能上传MS Word或pdf文件。函数"move_uploaded_file"根本不起作用。我已经检查了 php.ini 中的限制,检查了目标文件夹中的文件和文件夹权限,所有内容都经过谷歌搜索和验证,包括查看 SELinux 是否有任何问题,但它只是拒绝上传,也没有看到任何错误。现在感到沮丧,没有与EC2实例相关的回复在线提及该解决方案。请求专家提供帮助并了解下面介绍的代码中出了什么问题

我的是一个 T2 大型实例,带有 PHP 7.2.11、Apache/2.4.37(红帽企业 Linux(、mysqld 8.0.17,仍然不明白问题是什么,也没有错误记录错误.log

<?php
include 'includes/UploadException.php';
define ('SITE_ROOT', realpath(dirname(__FILE__)));
echo "Current User:".get_current_user()."<br />";
if ($_POST['hdnOp'] == "A"){
$file = $_FILES['resume'];
$filename = $_FILES['resume']['name'];
$fileTmpName = $_FILES['resume']['tmp_name'];
$filesize = $_FILES['resume']['size'];
$fileerror = $FILES['resume']['error'];
$filetype = $_FILES['resume']['type'];
if ($filetype=="application/msword" || $filetype=="application/pdf"){
$random = rand(1111,9999);
$newfilename = $random.$filename;
$uploadpath = "/BMT/UserProfiles/".$newfilename;
echo "<h2>FILE = ".$newfilename."</h2>";
echo "<h2>ULPATH = ".$uploadpath."</h2>";
move_uploaded_file($fileTmpName,$uploadpath);
if ($_FILES['resume']['error'] === 0){
echo "<h3>ALL OK</h3>";
echo "ERROR CODE = ".$_FILES['resume']['error']."<br />";
echo "File Name :".$newfilename;
echo "File Size :".$filesize." kb";
echo "File Type :".$filetype;
} else {
throw new UploadException($_FILES['resume']['error']);
}
}
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=yes">
<!-- Bootstrap CSS -->
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<title>UPLOAD</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
function submitForm(this){
return true;
}
</script>
</head>
<body class="container">
<table class='table table-bordered table-hover table-sm'><body>
<tr><td>
<form id="teacher-form" class="login-form" name="teacher-form" method="POST" enctype="multipart/form-data" action="test.php" onsubmit="return submitForm(this)">
<input type='hidden' name='hdnOp' value='A'>
<div id="form-content"></td></div><td></td></tr>
<tr><td>
<label>Upload Resume<b>*</b>&nbsp;&nbsp;<i>Note: Most recent upload will overwrite earlier version (if any)</i></label><br />
<input type="file" name="resume" id="resume" accept=".pdf,.docx,.doc,.rtf"><br /><div id='errresume'></div>
</td></tr>
<tr><td><input name="submit" type="submit" value="SUBMIT" /></td></tr>
</table>
</div>
</form>
</body>
</html>

我应该能够在/BMT/UserProfiles/文件夹中看到该文件

[root@ip-XXX-XX-XX-XXX bmt]# ls -al /BMT/UserProfiles/
total 4
drw-r--r--. 2 apache apache  24 Oct 16 12:17 .
drw-r--r--. 3 apache apache  26 Oct 16 12:05 ..

ADyson 终于设法解决了这个问题......

chcon -R --type httpd_sys_rw_content_t /BMT/UserProfiles/

然后停止并启动httpd服务 怀疑是否在某个地方 SELinux 负责......在此之后,现在它工作正常。感谢您的所有帮助和建议,真的帮助我从不同的角度进行调试

相关内容