How do I create file on server? - php

Let say I want create file call style.css in /css/ folder.
Example : When I click Save button script will create style.css with content
body {background:#fff;}
a {color:#333; text-decoration:none; }
If server cannot write the file I want show error message Please chmod 777 to /css/ folder
Let me know

$data = "body {background:#fff;}
a {color:#333; text-decoration:none; }";
if (false === file_put_contents('/css/style.css', $data))
echo 'Please chmod 777 to /css/ folder';

You can use the is_writable function to check whether the file is writable or not.
For example:
<?php
$filename = '/path/to/css/style.css';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'Please chmod 777 to /css/ folder';
}
?>

is the function you may want to use
http://php.net/manual/en/function.fopen.php to open the file
http://php.net/manual/en/function.fwrite.php to write your css
http://php.net/manual/en/function.fclose.php to close the file
or use
http://php.net/manual/en/function.file-put-contents.php just one command to master them all
if you fopen the file and the result of the operation is false then you can't write the file (maybe for permissions, maybe for UID mismatch in safe mode)
file_put_contents (php5 and upper) php calls fopen(), fwrite() and fclose() for you and return false if something id wrong (you should make yourself sure that false is really the boolean value though).

fopen with 'w' flag

<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
http://php.net/manual/en/function.fwrite.php | Example 1

Related

Why can't this header location thing works

function actionExit()
{
$fake = file_get_contents("fake.txt");
$header='Location: '. $fake;
file_put_contents ("data.txt",$header); //It shows Location: http://google.com
header($header);
exit();
}
The file data.txt contains Location: http://google.com
However, it doesn't show anything just a blank screen.
I think that you can't write your file due to missing permissions, so that file_put_contents() fails.
You can check it with is_writable()
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writeable';
} else {
echo 'The file isn\'t writeable';
}
If the file isn't writeable, you can set permissions with chmod()

PHP, Creating File in Directory

Help!
So I've look at many solutions of this on stack overflow, but non seem to have worked. So what I'm doing is creating a directory with a random number, then creating a text file in it.
My directory is like this:
localhost
--/tdir/
---/(random number directory) < this is were i want to save to the text file
Here is my code:
<?php
$dir = rand(1, 1999999);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
if ($dir == true) {
echo "Created directory! :D";
} else {
echo "Failed to create directory! ~ :(";
}
chmod("/tdir/$dir/", 0777);
echo "</br><a href='/tdir/'>Go Back</a>";
echo "</br><a href='/tdir/$dir'>Go to page!</a></br>";
$my_file = '../'.$dir.'/file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);
?>
I keep getting this error:
Created directory! :D
Go Back
Go to page!
Cannot open file: ../1320710 *random number directory* /file.txt
I need to be able to save to file.txt in the $dir directory!
You have to evaluate the response of the mkdir function, just write it like this
if (mkdir($dir, 0777, true) === TRUE){
....
Also, when writing/reading folders, make sure that you use System path, not URL path.

failed to open stream: No such file or directory

Can anyone help with this one? I am new to web developing and not sure what this error means?
Warning: fopen(images/nophoto.png): failed to open stream: No such
file or directory in /home/u835626360/public_html/remove.html on line
101
can't this file/picture is open you need close
CODE:
$expire=time()-3600;
setcookie("dname","a", $expire);
setcookie("dpode","a", $expire);
}
function delpics($filename)
{
$path_to_file='userpics/';
$old = getcwd(); // Save the current directory
chdir($path_to_file);
$fh = fopen($filename, 'w') or die("can't this file/picture is open you need close ");
fclose($fh);
if (!unlink($filename))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $filename");
}
chdir($old); // Restore the old working directory
}
You need to give fopen the full path of the file, and you don't need chdir() at all. Try this version:
$path_to_file='userpics/';
$fh = fopen($path_to_file.$filename, 'w') or die('Permission error');
I was facing same problem. I was thinking file will be created if I use w or w+ but giving me above error.
So problem was we need to create dir before we can create file.
We can get absolute DIR path of file
$dir = dirname($filename);
Create DIR
//if(!is_dir($dir))
mkdir( $dir , 0755, true);
Third parameter is important you want recursively
I know it sound stupid but it may save someone's time so added here
First make the dir manually in your server(if you have one) or local pc(if you dev in local)
Be sure to have write right for apache in your dir (0777 in unix-linux if you wan't to be sure you can do what you wan't and no idea for windows)
and then like it was said give the good path to fopen and not only filename
Try this:
$expire=time()-3600;
setcookie("dname","a", $expire);
setcookie("dpode","a", $expire);
function delpics($filename)
{
$path_to_file='/userpics/';
$old = getcwd(); // Save the current directory
chdir($old.$path_to_file);
$fh = fopen($filename, 'w') or die("can't this file/picture is open you need close ");
fclose($fh);
if (!unlink($filename))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $filename");
}
chdir($old); // Restore the old working directory
}

php file is writable but cannot be deleted

I am using this function. is_file and is_writable return true, but when I true to unlink, it gives an error. This is on windows server.
if(is_file($fileToDelete)) {
if(is_writable($fileToDelete)) {
unlink($fileToDelete);
}
}
The file is a PDF document, which I have open. I thought is_writable would return false in this case, but it doesn't.
So how can I tell if a file can be deleted or not?
Thank you
What about doing it the other way around? Just try to delete the file and check whether it is really gone?
#unlink($fileToDelete);
if(is_file($fileToDelete)) {
// file was locked (or permissions error)
}
Not sure whether this is workable in your specific case though, but judging by the code in your question this should be what you want.
Are you using the file? I mean, did you open it by doing fopen($file)?
Do a fclose($file) before trying to delete the file.
For them who don't want to delete the file before the check, the solution is here :
$file = "test.pdf";
if (!is_file($file)) {
print "File doesn't exist.";
} else {
$fh = #fopen($file, "r+");
if ($fh) {
print "File is not opened and seems able to be deleted.";
fclose($fh);
} else {
print "File seems to be opened somewhere and can't be deleted.";
}
}
Simple, and efficient.

chmod is not working on php and I can not write a file!

Hi I want to write in a file with php, but I can not write anything. When I write this test function I got "Cannot change the mode of file deneme" error. How can use chmod and so that I can write into a file thanks for helping..
<?php
function file_write($filename, $flag, &$content) {
if (file_exists($filename)) {
if (!is_writable($filename)) {
if (!chmod($filename, 0666)) {
echo "Cannot change the mode of file ($filename)";
exit;
};
}
}
if (!$fp = #fopen($filename, $flag)) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($fp, $content) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
if (!fclose($fp)) {
echo "Cannot close file ($filename)";
exit;
}
}
$a="osman"; file_write("deneme", "w", &$a);
?>
Refer to php manual and chmod() function http://php.net/manual/en/function.chmod.php as you need write permissions for that folder/file
I guess tha main thing is "The current user is the user under which PHP runs." Can you perform chmod on the file you've created via php script?
I am not sure if you looked into umask.

Categories