【问题标题】:Unable to upload file from PHP to Centos server?无法将文件从 PHP 上传到 Centos 服务器?
【发布时间】:2019-08-09 16:01:18
【问题描述】:

我在将文件从 PHP 上传到 Centos 7.0 Apache 服务器时遇到问题

PHP 代码

  <html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="index.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>
<?PHP
  if(!empty($_FILES['uploaded_file']))
  {
    $path = "/var/www/html/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>

请注意,file_uploads 在 php.ini 中是开启的

文件夹“/var/www/html/”的权限是0777,它的所有者是apache

我在stackoverflow中尝试了几乎所有的解决方案,

chmod -R 0777 /var/www/html

chown apache /var/www/html

find /var/www/html/ -type f -print0 | xargs -0 chmod -v 777 

仍然文件上传失败,并在 HTTPD error_log 中出现此错误

[Tue Mar 19 10:34:21.257969 2019] [:error] [pid 27446] [client 172.20.20.41:61798] PHP Warning:  move_uploaded_file(/var/www/html/Scan.pdf): failed to open stream: Permission denied in /var/www/html/index.php on line 23, referer: http://10.104.25.70/index.php
[Tue Mar 19 10:34:21.258095 2019] [:error] [pid 27446] [client 172.20.20.41:61798] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/php64POiK' to '/var/www/html/Scan.pdf' in /var/www/html/index.php on line 23, referer: http://10.104.25.70/index.php

输出

ls -l 

drwxrwxrwx. 3 apache root 34 Mar 18 13:24 html

输出

stat html

  File: ‘html’
  Size: 34              Blocks: 0          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 67281951    Links: 3
Access: (0777/drwxrwxrwx)  Uid: (   48/  apache)   Gid: (    0/    root)
Context: system_u:object_r:httpd_sys_content_t:s0
Access: 2019-03-19 09:09:19.056277906 +0300
Modify: 2019-03-18 13:24:45.504174727 +0300
Change: 2019-03-19 10:37:18.901089085 +0300
 Birth: -

【问题讨论】:

  • 只是为了确保检查您尝试移动的文件的权限,而不是文件夹本身。

标签: php apache file-upload centos


【解决方案1】:

Selinux需要配置上下文

cd /var/www/html
mkdir upload
chown apache:apache upload
chcon -t httpd_sys_rw_content_t upload
ls -Z /var/www/html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2014-04-04
    • 2013-07-09
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多