Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 720
thehindutimes Avatar asked Sep 02 '14 18:09

thehindutimes


People also ask

Does Microsoft IIS support PHP?

The fastest and easiest way to install PHP on Internet Information Services (IIS) is by using the Microsoft® Web Platform Installer (Web PI). Web PI completely automates setting up IIS, FastCGI, and the latest version of PHP from the php.net Web site.

Is IIS PHP editor code?

IIS only runs on Windows, but keep in mind that running PHP on IIS is not the same as running PHP on Windows. There are options to run PHP on Windows like XAMPP or WampServer. However, these two options make some additional choices for you. They run Apache as a web server and use MySQL or MariaDB as a database server.


1 Answers

This kind of problem occurs because of three reason only:

  1. Don't have proper Directory owner.Ex:- In Apache default user is www:data.
    • First check the directory owner.
  2. You don't have sufficient write permission for directory i.e. 755
    • Check directory permission
  3. 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 AppPool\DefaultAppPool Ex. e.g. IIS AppPool\yourdomain.com. This setting is required for IIS8.

  1. First try to change the owner to IUSER. It should work according to php document.
  2. 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.

Change user group  & permission

like image 69
Rajesh Ujade Avatar answered Oct 29 '22 00:10

Rajesh Ujade