How to set the properties of a file to 0777? - php

How can I set the properties of a file to 0777 which i created through php create file?
$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = 'test';
fwrite($fh, $stringData);
fclose($fh);

chmod("/somedir/somefile", 0777);
according to: http://php.net/manual/fr/function.chmod.php

You cannot.
You have 2 alternatives - to set it after with chmod() or use umask() before. In the latter case you need to restore it back to not break other code, that relies on the original umask value (if any).

You can protect your file using chmod. Here is an example:
chmod($myFile, 0777);
Take a look at chmod.

Related

php is unable to write to file

This is on Windows Small Business Server 2011 Essentials running iis7
The following code always returns "unable to write"
<?php
$myFile = "http://www.ascbits.com/test/test.txt";
if (is_writable($myFile)) {
$fh = fopen($myFile, 'a');
}else{
die("unable to write");
}
$body = "test ";
fwrite($fh, $body);
fclose($fh);
?>
I've checked the permissions on the file and it looks like I should be able to write to it.
Any suggestions?
try:
$myFile = $_SERVER['DOCUMENT_ROOT'] . "/test/test.txt";
The PHP streams layer is what allows you to read and write to URLs. If you check the documentation on the http stream wrapper, you'll see that it's read-only:
Allows read-only access to files/resources via HTTP 1.0, using the HTTP GET method.
It looks like you're just trying to write to a local file. Reference it by path, depending on where it's located the following may work:
$myFile = $_SERVER['DOCUMENT_ROOT'] . "/test/test.txt";
$myFile = "test/test.txt";

(php) fwrite erase and doesn't write

I have a file "tw.txt" with the text "test text" in it.
If I try to write "lol" in "tx.txt" with fwrite, the content ("test text") is simply erased and not replaced.
There is no error displayed by the server, however, I can see my Error: can't write in file.
CHMOD is set to 777 in every files and folders, from the "var" rep to the website folder. If I try to read a file with fopen, no problem. I tried to change the chmod with PHP... no success. I tried to append, it erase.
The code works fine on two other servers.
Any clues ? Thanks.
<?php
ini_set('display_errors', 'On');
ini_set('allow_url_fopen', '1');
error_reporting(E_ALL);
$fd=fopen("tw.txt","w") or die("Error: can't open file.");
//chmod("tw.txt", 511);
fwrite($fd,"lol") or die('Error: can't write in file.');
fclose($fd);
?>
Have you tried other opening modes?
If you need to append some data, you should try something like:
$fd=fopen("tw.txt","a+")
$myFile = 'tw.txt';
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, 'lol');
fclose($fh);

PHP Write File Error

I have a page called index.php which is calling a function "writelog" in includes/Logger.php
I have file located at includes folder and code is as below.
function writelog($logText){
$myFile = "testlog.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $logText + "\n";
fwrite($fh, $stringData);
fclose($fh);
}
It shows errror "can't open file" . I have set FullPermission for everyone and still it says it cant access file.I tried to put file in same folder as index.php and same error. What can be possible cause ? Am I having wrong path ?
Try using the full path of the log file
$myFile = "/full/path/to/testlog.txt";
I am assuming this file is also in includes, I'm guessing this is called from another script so the path would be one of the calling script. You can use this:
$prevdir = getcwd();
chdir(dirname(__FILE__));
$myFile = "testlog.txt";
chdir($prevdir);
But it's best to use absolute paths

php write file and set permission

I'm trying to create a php file which I can edit straight away without manually set the permissions.
I'm trying this...
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
?>
...it creates the file, but when I try to edit the file in my IDE it won't let me of course. I have to manually set the permission of the file created. Is there any way I can create the file and have the permission already set?
Thanks in advance
Mauro
Yes, you can thanks to PHP CHMOD
// Read and write for owner, read for everybody else
chmod("/somedir/somefile", 0644);
Since this aspect wasn't covered in previous answers I'll add it here:
chmod() will only take a path string as the 1st argument. So you cannot try to pass to resource that was open with fopen(), in this case $fh.
You need to fclose() the resource and then run chmod() with the file path. So a proper practice would be storing the filePath in a variable and using that variable when calling fopen() rather than passing it a direct string in the first argument.
In the case of the example code in the answer this would simply mean running chmod($myfile, 0755) (the permission code is only an example and be different of course.)
full code after corrections:
<?php
$var = '<?php $mycontent = new Content(); echo $mycontent->block($p_name);?>';
$myFile = "testFile.php";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $var;
fwrite($fh, $stringData);
fclose($fh);
// Here comes the added chmod:
chmod($myFile, 0755);
?>
Php has chmod, works just like the Linux version.

php: writing files

I want to create a file on the webserver dynamically in PHP.
First I create a directory to store the file. THIS WORKS
// create the users directory and index page
$dirToCreate = "..".$_SESSION['s_USER_URL'];
mkdir($dirToCreate, 0777, TRUE); // create the directory for the user
Now I want to create a file called index.php and write out some content into it.
I am trying:
$ourFileName = $_SESSION['s_USER_URL']."/"."index.php";
$ourFileHandle = fopen($ourFileName, 'x') or die("can't open file");
fclose($ourFileHandle);
// append data to it
$ourFileHandle = fopen($ourFileName, 'a') or die("can't write to file");
$stringData = "Hi";
fwrite($ourFileHandle, $stringData);
But it never gets past the $ourFileHandle = fopen($ourFileName, 'x') or die("can't open file"); Saying the file does not exist, but that is the point. I want to create it.
I did some echoing and the path (/people/jason) exists and I am trying to write to /people/jason/index.php
Does anyone have any thoughts on what I am doing wrong?
PHP 5 on a linux server I believe.
-Jason
First you do :
$dirToCreate = "..".$_SESSION['s_USER_URL'];
But the filename you try to write to is not prefixed with the '..', so try changing
$ourFileName = $_SESSION['s_USER_URL']."/"."index.php";
to
$ourFileName = '..' . $_SESSION['s_USER_URL'] . '/index.php';
or probably tidier:
$ourFileName = $dirToCreate . '/index.php';
You are probably getting the warning because the directory you are trying to write the file into does not exist
It could be a result of one of your php ini settings, or possibly an apache security setting.
Try creating the dir as only rwxr-x--- and see how that goes.
I recall a shared hosting setup where "safemode" was compiled in and this behaviour tended to occur, basically, if the files/dirs were writable by too many people they would magically stop being acessible.
Its probably doc'd in php, but ill have to check.
why not use:
file_put_contents( $filename, $content )
or you could touch the file before writing to it.
Does the file 'index.php' already exist? When you fopen with the 'x' mode, if the file exists fopen will return FALSE and trigger a warning.
What i first noticed is you are making a directory higher in the tree, then attempting to make the php file in the current folder. Correct me if i'm wrong, but aren't you trying to make the file in the new created folder? if i recall php correctly (pardon me it's been a while, i'll probably add something from another language in here not noticing) here is an easier to understand way for a beginner, of course change the values accordingly, this simply makes a directory and makes a file then sets permissions.
<?php
$path = "..".$_SESSION['s_USER_URL'];
// may want to add a tilde (~) to user directory
// path, unixy thing to do ;D
mkdir($path, 0777); // make directory, set perms.
$file = "index.php"; // declare a file name
/* here you could use the chdir() command, if you wanted to go to the
directory where you created the file, this will help you understand the
rest of your code as you will have to perform less concatenation on
directories such as below */
$handle = fopen($path."/".$file, 'w') or die("can't open file");
// open file for writing, create if it doesn't exist
$info = "Stack Overflow was here!"; // string to input
fwrite($handle, $info); // perform the write operation
fclose($handle); // close the handle
?>

Categories