Autorize php to modify a text file on host - php

I have a php script on my webhost, GoDaddy, and I want to modify a text file. But it gets blocked, I think it's a problem of authorization but I don't know how to solve this.
<?php
$var= "test";
$fileopen = fopen("file.txt", w+);
fwrite($fileopen,$var);
fclose($fileopen);
?>

Apparently the issue is not with PHP but with the access control management on the server. You don't tell us the error you get, but I guess that you are denied write access to those files. Check the rights of the corresponding files and directory on your server and tweak them as needed to let the web server process access those files.
How to do it is very much dependent on the server's operating system, on the identity under which the web server process is running and on the kind of administrative rights you have yourself. Without further information, it is difficult to say more. Your provider must have some kind of documentation about how you can set up directory and file access rights.

Related

Can't write to a remote server location with PHP but I can browse and write to it in File Explorer?

Sorry if this a repeat of a question that has been asked before but I have not been able to find my exact situation. We are trying to migrate our website server from a Windows 10 VM (yes I know) to a Windows Server 2019 VM. We have some PHP on our site that writes files to some of our other servers on the same domain and have been able to do so without issue using file_put_contents like so:
file_put_contents("\\\\server\\folder\\folder\\folder\\".$filename, $file);
Now all of a sudden, to run the same code on our new server I get a Warning on this line, "Failed to open stream: Permission denied". I have permission to access this folder, I can browse to \server\folder\folder\folder and create a file there. I even tried mapping this server to a letter drive on my new web server, and still same error. I can put the file on the local C drive just fine but that's it.
Running fileperms on the folder path gives Warning: fileperms(): stat failed. Running is_writable on the folder path returns false, I just can't see how. Running it on the old Windows 10 web "server" returns true. I've read some things about needing to enable certain settings on the server you're trying to access, but I just can't think of what would allow one VM to access it and not another. Both VM's are logged in with the same user with admin rights. I can bring up the same folder in file explorer and write to it, just not via PHP. What obvious thing am I missing?
Thanks!
After weeks of banging our heads against the wall we finally figured this out. On our old web server, when right-clicking the website from the Sites file tree in IIS, under Manage Website -> Advanced Settings, the Physical Path Credentials field was set to the credentials needed to access these folders. On the new server it was blank. I'm not sure how this got missed but in any case, after entering the correct credentials here everything immediately worked.

Why is PHP failing to copy a file to a shared folder in a LAN?

I need to copy a file from one computer to another computer which is connected to lan network. I have IP address of that two machines. I have used code like this to achieve it ,
$file = 'file.text';// inside htdocs folder
$newfile = '\\\\192.168.1.15\\htdocsfolder\\';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
but copy failed. How to debug, why file is not copied to another computer htdocs folder? whether i need to change share access on both computers?
Please help.
Thanks
You seem to be on a windows machine. Here are a couple of things to keep in mind and check for (which are a variation of the same theme):
If this is a scheduled task/batch job, try executing it from the terminal and check any errors you might get (beware of relative paths):
C:\> path-to-php script.php
Otherwise, if you can, run it as the webserver's user and see below:
Webserver process needs to have write access to \\192.168.1.15\htdocsfolder and read access to htdocs folder.
Check the user your webserver process runs as. Use your favorite process/task/service manager to check for the exact username and see if this user has the required access modes.
If you are in an Active Directory environment give access to the username identified above
If OTOH you are in a LAN, make sure the username exists (is a valid user) and has the same password on both machines.
If you do this right, permissions for Everyone are not necessary (and usually a bad thing)
Not to pick on MS and Windows, but do make sure about shares and permissions from "Manage my computer" -> "System Tools" -> "Shared Folder" as specified by Microsoft
Debugging is hard, you'll have to do a lot of further research on your own, Google and SO are your friends.
please check that the user executing php script has the access on remote file system.
If scripts are executed on Linux, its useful to mount remote share.
Just to add, your code worked fine with my case.

Using a text files to store data

I want to to read and write some data to text files from my PHP app. Its a small amount of data. Really just configurations.
My concern is that I have no control over the deployment environment. It will be a mixed bag of servers, mac and windows. They will all be running PHP5.3 and greater. The mac servers will have apache, and the windows servers will be running IIS.
I don't want customers calling with issues related to server setup and or permissions on files/folders.
I am pretty sure that WordPress does this all the time so, I know this is possible to do cleanly. The questions is how? Does anyone have any suggestions, pointers to libraries, or strategies that will help me accomplish my goal.
I ruled out sqlLite for this purpose, because i don't think it is enabled by default on windows and I think it is no longer installed on php5.4 windows by default. My main goal is to be able to persist a small amount of data in such a way that does drive my support costs through the rough the roof.
The way all common big PHP projects do it afaik is simply by reserving a folder for it, and checking its permissions on install.
You could easily make a /gen or /data folder in your webroot, and on install/update check that it:
contains a .htaccess file stating deny from all if the webserver is Apache, or an equivalent method of protection (just file_get_contents via the public URL to test) on other webservers
is_writable (you could also write, read and delete a small sample file to ensure this)
Put your documents in there and it's safe and portable on every platform.
Some sample code:
$docroot = $_SERVER['DOCUMENT_ROOT'];
$dataroot = $docroot.'/data';
$testfile = $dataroot.'/test.txt';
$publicURL = $youHaveThisSomewhere.'/data/test.txt';
if(!is_dir($dataroot))
die("The required data folder is not present at $dataroot");
if(!is_writable($dataroot) || file_put_contents($testfile, 'test') === FALSE)
die("Data path ($dataroot) is not writable, make it so!");
if(file_get_contents($publicURL) !== FALSE)
die("Data path is publicly accessible, go fix it!");
if(!unlink($testfile))
die("I also need delete rights in the data folder!");
die("Installation successful!");
Yeah, but even with Wordpress you have to worry about making certain files and directories writable (chmod, windows file permissions, etc).
And if I'm not mistaking Joomla/Wordpress (one of them at least) also give you the opportunity to enter FTP credentials, so rather than editing the file through the filesystem, it will try to upload the edited version through the FTP server.
What you simply could do is have a config file that must be edited in a text editor. And have the text-file read only for the webserver / application. But then changing one of these setting cannot be done through the website itsself, but needs a person to edit the text file in a text editor.
If you are already using a database, then ONLY the database-settings would have to be in the config file. The rest can be stored in the database.
I think the option you may be looking for involves the following steps:
Create an install file in this install script file:
See if directory is writable
If not ask for FTP credentials
Determine the system type Windows or *Nix bassed
Establish an FTP connection
*nix variants send a CHMOD command Windows send a CACLS command to make the directory writable
Terminate the FTP command

PHP and NAS (IIS 7.5, Windows Server 2008)

how can i access folders like
\\10.200.0.3\some_folder
user name: user
password: pass
is it possible to map network device then access it using php?
please help me connect to this share and access files there..
Try one of these;
Make sure the IIS worker process has access to that location (IUSR or IIS_IUSRS)
Change the website's application settings in IIS to use credentials from a user account who has access to the location
Since you will be running PHP using IIS/Apache user make sure server user also has permissions to access network shared folder. Once you are sure IIS user does have permissions to access shared folder, you may try listing contents of shared folder by using path such as:
$target_path = '\\\\server\\images\\';
Have a look at this questions, might be helpful:
PHP: upload files to network shared folder
What you're looking for is Server Message Block (SMB) or the open source implementation SAMBA.
To use this in PHP, you could look at smbwebclient.
You might however simply add a drive map to the share on your Windows server, assign it a fixed letter (like Z:) or even simply access \\hostname\folder (escape the backslashes) and use the PHP built in directory commands to read and write the files from that drive. It'll be a helluvalot easier.

file permissions in php

I have created a file called config.php and it is as under
<?php
$dbUser="test";
$dbPassword="123456";
$dbName="testDatabase";
$dbHost="localhost";
$dbPort="3306";
$tablePrefix="test_";
?>
Now i have set permissions of this file to 744, so that i can make connection any time i need.
but what would happen if anyone else will try to read this file and make connection to my database server as a dbuser. this would be problematic for me.
So what should be done?? or what permissions should be given so that my web server will be able to read the file but anyone else who tries to read should not be able to read the file.
Thanks in advance.
If you have your own web-server, running under your own user, you can make it 600.
But usually you don't have to worry about it, because your shared hosting provider taking care of it, making different hosting users unable to access each other's files.
When you have a file like config.php you don't have to worry about other people who can access it. First of all people can't read the php code inside your document so you dont have to edit the permissions of the file.
Leave it as default.

Categories