PHP scripts writes to file on localhost but not on IIS server
Asked Answered
T

3

7

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

Trematode answered 2/9, 2014 at 18:46 Comment(5)
What error message did you recieve? What part of the script is not working. the open part or the write part. Have you tried running the IIS as administraterLudwick
Don't have permission to the server other than connecting to it via Remote Desktop. And I'm not getting any error message. The data is just not written to the file.Trematode
Do you have sufficient file permissions to write to the file?Ludwick
If I check the File Permissions in FileZilla they all have Read, Write and Execute permissions on Owner, Group and Public @YahyaUddinTrematode
@can who is the directory owner?Manysided
M
3

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

Manysided answered 10/9, 2014 at 16:27 Comment(4)
Sounds like you know your stuff! Where do you check step 1?Trematode
@thehidutimes Added screenshot for your query. Go to your server using remote desktop sharingManysided
or ask your system admin to do this operationManysided
This is either permission or path issue. I am not sure but u can check openbasedir restriction as well.Restrained
L
0
  1. 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.

  2. 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);

  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.

  4. 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.

  5. 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.

Ludwick answered 3/9, 2014 at 8:47 Comment(7)
Ok, thanks. I´ll try it. I did try to open the php script in the browser by enetering the url to it and that gave me the die command message. Does that mean point 5 is the problem maybe?Trematode
Yes! Make sure the file exist realative to your file.Ludwick
Changed $myFile = "json/countries.json"; to $myFile = "../json/countries.json"; but it is still not working. The php scripts are in a php folder and the json in a json folderTrematode
Try using var_dump(file_exists ( $filename ) ) to see if ur file path is valid. It may be that your writing the file path wrong or relative to the wrong file. It should output true if it exists.Ludwick
Also check your file permissionsLudwick
Where will var_dump output if the file exists or not? I'm not seing anything and now it has even stopped working with xampp on localhost :S @Yahya UddinTrematode
it should output the word "True" if it exits. "False" otherwise. Or you might want to try this: if (file_exists ( $filename ) ) {echo "file exists"; } else {echo "file does not exist";} Tell me what output you getLudwick
H
0

Add IIS_IUSRS with access "execute, list, read" to security tab on properties.

Halfcock answered 11/9, 2014 at 22:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.