简体   繁体   English

move_uploaded_file 上传 function 在 AWS EC2 实例中失败,并显示消息“无法打开 stream:权限被拒绝”

[英]move_uploaded_file upload function fails in AWS EC2 Instance with message “failed to open stream: Permission denied”

I have a simplest php page for just uploading only MS Word or pdf files by a user.我有一个最简单的 php 页面,用于仅由用户上传 MS Word 或 pdf 文件。 The function "move_uploaded_file" simply DOES not work. function“move_uploaded_file”根本不起作用。 I have checked the limits in php.ini, checked with file and folder permissions in the target folder everything googled and verified, including to see if SELinux has any issues but it simply refuses to upload and neither is there any error seen as well.我检查了 php.ini 中的限制,检查了目标文件夹中的文件和文件夹权限,所有内容都通过 Google 搜索和验证,包括查看 SELinux 是否有任何问题,但它只是拒绝上传,也没有看到任何错误。 Frustrated now and nowhere replies related to EC2 instance are mentioning the solution online.现在很沮丧,与 EC2 实例相关的回复无处提及在线解决方案。 Request experts to help and understand what is going wrong in the code presented below请求专家帮助并了解下面提供的代码中出了什么问题

Mine is a T2 large instance with PHP 7.2.11, Apache/2.4.37 (Red Hat Enterprise Linux), mysqld 8.0.17, still not understanding what is the issue, and no error is logged in error.log as well我的是一个 T2 大型实例,带有 PHP 7.2.11,Apache/2.4.37(Red Hat Enterprise Linux),mysqld 8.0.17,仍然不明白是什么问题,并且在 error.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>

I should be able to see the file in /BMT/UserProfiles/ folder我应该能够看到 /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 finally managed to get the solution to this problem.... ADyson终于设法解决了这个问题......

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

Then stop and start the httpd service Had a doubt if somewhere SELinux was responsible... after this now its working fine.然后停止并启动 httpd 服务 怀疑 SELinux 是否负责……在此之后现在它工作正常。 Thanks for all the help and suggestions from your end, really helped me to debug from different perspective感谢您提供的所有帮助和建议,真的帮助我从不同的角度进行调试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 move_uploaded_file(uploads /)[function.move-uploaded-file]:无法打开流:权限被拒绝 - move_uploaded_file(uploads/) [function.move-uploaded-file]: failed to open stream: Permission denied move_uploaded_file 无法打开 stream:权限被拒绝 - Mac - move_uploaded_file failed to open stream: Permission denied - Mac move_uploaded_file无法打开流和权限被拒绝错误 - move_uploaded_file failed to open stream and Permission denied error move_uploaded_file 无法打开流:权限被拒绝 Mac - move_uploaded_file failed to open stream: Permission denied Mac move_uploaded_file给出:无法打开流:权限被拒绝 - move_uploaded_file gives: failed to open stream: Permission denied php 文件上传错误警告 move_uploaded_file 无法打开 stream 权限被拒绝 - php file upload error warning move_uploaded_file failed to open stream permission denied in 上传文件警告:move_uploaded_file(../ view / pictureswhy.PNG):无法打开流:权限被拒绝 - Upload file Warning: move_uploaded_file(../view/pictureswhy.PNG): failed to open stream: Permission denied PHP警告:move_uploaded_file():无法移动/无法打开流:权限被拒绝 - PHP Warning: move_uploaded_file(): Unable to move/failed to open stream: Permission denied 警告:move_uploaded_file无法打开流:权限被拒绝的PHP文件 - Warning: move_uploaded_file failed to open stream: Permission denied PHP file Laravel中出现“ move_uploaded_file():无法打开流:权限被拒绝”错误 - Getting “move_uploaded_file(): failed to open stream: Permission denied” error in Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM