dongtan5811 2019-03-19 07:36
浏览 239
已采纳

无法将文件从PHP上传到Centos服务器?

I am having trouble uploading a file from PHP to Centos 7.0 Apache Server

PHP CODE

  <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!";
    }
  }
?>

Please note that file_uploads is on in php.ini

and the folder "/var/www/html/" has permission 0777 and the owner of it is apache

I tried almost all solutions in stackoverflow,

chmod -R 0777 /var/www/html

chown apache /var/www/html

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

Still file upload fails, and got this error in 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

the output of

ls -l 

is

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

the output of

stat html

is

  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: -
  • 写回答

1条回答 默认 最新

  • dongyuxiao6295 2019-03-19 07:45
    关注

    Selinux needs to be configured context

    cd /var/www/html
    mkdir upload
    chown apache:apache upload
    chcon -t httpd_sys_rw_content_t upload
    ls -Z /var/www/html
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵