首页 > 解决方案 > PHP 将文件写入 localhost 但不在 IIS 8 上

问题描述

请协助,我正在将文件写入我的applicationdir/uploads/文件夹。通过在我的本地主机上写出文件时wamp64 host,文件会完美生成。

但是,当我在我IIS 8.0 server的 .

我正在使用File_Put_Contents命令来创建文件。

我已经阅读了其他帖子,并且正如其他人所指出的那样,我已将我的applicationpool\MYAppPool名字更改为类似于应用程序名称(无济于事)。我还允许iis_iusrs写入目录的权限。

有什么想法吗?下面的PHP:

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['xml'] ) ){
        $Counter = date("Y-m-d") . ' ' . Time();
        $file  = 'uploads/' . $Counter . ' - Export.xml';
        $bytes = file_put_contents( $file, $_POST['xml'] );
        exit( sprintf('%d bytes written to %s',$bytes,realpath($file) ) );
    }
?>

截屏:

提交时

我看不出这会有什么不同,但这里是将 xml 数据传递给服务器的 javascript 代码。Javascript:

<script>
   var template = [
              '<?xml version="1.0"?>',
              '<unattend xmlns="urn:schemas-microsoft-com:unattend">',
              '<Data>',
              ' <SubmitBy><?SubmitBy?></SubmitBy>',
              ' <Level10><?Level10?></Level10>',
              ' <Level1><?Level1?></Level1>',
              ' <Level2><?Level2?></Level2>',
              ' <Level3><?Level3?></Level3>',
              ' <Level4><?Level4?></Level4>',
              ' <Title><?Title?></Title>',
              ' <Subject><?Subject?></Subject>',
              ' <Author><?Author?></Author>',
              ' <Keywords1><?Keywords1?></Keywords1>',
              ' <Keywords2><?Keywords2?></Keywords2>',
              ' <Destroy_Date><?Destroy_Date?></Destroy_Date>',
              '</Data>',
              '</unattend>'
            ].join('\r\n');


            const ajax=function( params ){
                with( new XMLHttpRequest() ){
                    onreadystatechange=function(e){
                        if( this.status==200 && this.readyState==4 ){
                            alert( this.response )
                        }
                    }
                    open( 'POST', location.href, true );
                    setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                    send( params );
                }
            }

            document.addEventListener('DOMContentLoaded', ()=>{
                document.querySelector('input[ type="button" ]').addEventListener('click', e=>{
                    let vars={};                    
                    Array.prototype.slice.call( document.querySelectorAll('form > input[ type="text" ]') ).forEach( field =>{
                     vars[ field.name ]=field.value
                      });
                       Array.prototype.slice.call( document.querySelectorAll('select') ).forEach( field =>{
                     vars[ field.name ]=field.value
                      });
                         Array.prototype.slice.call( document.querySelectorAll('form > input[ type="date" ]') ).forEach( field =>{
                     vars[ field.name ]=field.value
                      });
                    let xml=template.replace(/<\?(\w+)\?>/ig, (match,name)=>{
                        return vars[name];
                    });
                    ajax.call( this, 'xml='+xml);
                })
            });
        </script>

标签: phpiisiis-8file-put-contents

解决方案


推荐阅读