PHP scripts writes to file on localhost but not on IIS server

I've made a simple website that works fine on localhost. So I've put it on an IIS Windows 2008r2 server and now my PHP scripts don't write to my JSON files anymore. I've checked the server and PHP is installed on it so I don't really know what's wrong or where to look.

I'm still not getting it to work so thought I'd explain the situation in more detail.

So this script works on localhost but not on IIS server.

<?php
$myFile = "../json/countries.json";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = json_encode($_POST["data"]);
fwrite($fh, $stringData);
fclose($fh)
?>

I've tried adding:

error_reporting(E_ALL);
ini_set('display_errors', '1');

and

chmod("../json/countries.json", 0644);

to the php but not seeing any different results or any errors.

Here's the javascript function that starts the process, and outputting the object to the console does show the correct data to be saved.

function saveJson(object, file) {

console.log("Saving JSON data: " + JSON.stringify(object));

$.ajax
({
    type: "POST",
    dataType : 'json',
    async: false,
    url: file,
    data: { data:object },
    success: function () {console.log("Thanks!"); },
    failure: function() {console.log("Error!");}
});

}

Still the json files are not being changed. Anyone good with Windows Server and php that might know why?

Thanks


This kind of problem occurs because of three reason only:

  • Don't have proper Directory owner.Ex:- In Apache default user is www:data.
  • First check the directory owner.
  • You don't have sufficient write permission for directory ie 755
  • Check directory permission
  • Path is incorrect to a directory to write or upload file.
  • Check the directory path where you are writing the file.
  • According to php document Installation on Windows systems default user is IUSER

    Hence you have to set the directory owner 'IUSR' is part of the IIS_IUSRS group, If its not works then try to set 'IIS AppPool{YouApplicationPoolName}' from IIS AppPoolDefaultAppPool Ex. eg IIS AppPoolyourdomain.com . This setting is required for IIS8.

  • First try to change the owner to IUSER. It should work according to php document.
  • Set the write permission for the directory.
  • How to Change a directory permission & user group on windows system.

    Rightclick on directory->security->edit
    

    Please check attached screen shot.

    更改用户组和权限


  • What version of PHP are you using. If it is less than 5.2, the json encode function may not be working. Use phpinfo() to find out.

  • It may also be that $_POST['data'] does not exist. try putting a array instead to see if thats the problem. Like: array(1,2,3);

  • Also if no warnings are being shown use: error_reporting(E_ALL); OR error_reporting(-1); at the top of your script. It may be that your host is turning off these errors. this will allow you to see what the problem is.

  • The host also has the ability to deactivate certain PHP function. Contact your customer service regarding this problem. Although I find it very unlikely that they have deactivated the ones you are using.

  • Make sure the file exist relative to where the PHP code is being executed. If this is the problem, the die command should run. You can see f the file exists using:

    if (file_exists ( $filename ) ) echo "file exists";

    else echo "file does not exist";

  • If none of the above work then I haven't got a clue! Sorry.


    将具有访问权限的IIS_IUSRS添加到属性的安全选项卡“执行,列表,读取”。

    链接地址: http://www.djcxy.com/p/22620.html

    上一篇: iMacros等待页面加载

    下一篇: PHP脚本写入本地主机上的文件,但不写入IIS服务器