Home > front end >  PHP - fopen("filename", "w") creates file in local environment but doesn't
PHP - fopen("filename", "w") creates file in local environment but doesn't

Time:02-01

I have a website with a hosting server running PHP. I have written an experiment that retrieves data through a survey model, and I use the fopen("filename", "w") function to store retrieved data.

I tried this in my local environment while building up the webpage. Everything worked fine, and a document was created in the subfolder TestResponses/ with the data requested.

I just uploaded the file in my server, and tried it out. Everything works fine except that my file is not created or stored.

I use the following code to submit my data:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "storedata.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("test_variables=some_variables");

and I use the following code to write it down in a file to be stored in my subfolder:

$test_variables = $_POST["test_variables"];

$myfile = fopen( "TestResponses/" . substr($test_variables, 0, 18) . ".txt", "w") or die("Unable to open file!");
fwrite($myfile, $test_variables);
fclose($myfile);

Does anybody know if I missed to do something required to create and store the file in the server?

P.S. the subfolder TestResponses/ already exists in the server, so it does not to be created.

P.P.S. I don't get any error message by pressing F12.


update: I discovered that the fopen() function works correctly and files are updated to the folder in the server side. It was a delay problem, and files were uploaded (visible) only after about 5 minutes. So, I will mark this question solved.

CodePudding user response:

The local environment and the remote server are different (duuh :) ).

Your first step is to figure out what is the user (on remote server) that runs your php file. What OS is the remote server? If it is linux, probably your user that runs php is www-data (apache) if you are running a version of ubuntu/debian.

If you have console access to the remote server you can check the permission of the folder TestResponses using ls -alh command in the parent folder of TestResponses.

HOW TO TEST

The first and the simplest thing is to chmod 777 TestResponses folder - but this is a security risk, only use the 777 permission to test that your data is created inside the folder.

If you use a software like filezilla or winscp, you can also check what permissions the TestResponses folder has.

The correct permissions for a folder should be 775 or 755 (depending on the group). You might need to check how permissions RWX are on linux (if linux is your remote server OS).

P.S. make sure you also add encoding UTF-8 to your website.

CodePudding user response:

On outside servers, always check write access using is_writable() on the folder. If there's no write access, check the rights of the folder with some FTP client or console (see @Mike 's answer), and if it's 775, make sure your FTP user is in the same group as www-data (or whatever the web server is running with). If you're not sure how, talk to the Linux guy operating the server.

If it's 755, you can only write with the same user (not group) that created the folder - so in this case, if it's an option to delete the folder (from FTP or console) and re-create it with your php script, it can be a good solution. (Obviously, if there's something in the folder or if it's something used by others, not just you, well, then please don't delete it)

  •  Tags:  
  • Related