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.
Related
How can I create a file on the server in the directory I want using php? I found the function fopen but it won't help me, because we cannot choose the directory.
Example:
$href = '/contact/downloads/227856/file2.php'
if (file_exists($href)) {
echo "File exists";
} else {
echo "File doesn't exist, but we will create it";
// And there we gonna create the file in the directory $href
}
$path_to_file = " " //Make sure $path_to_file is the full path, something like /home/user/public_html/etc/ <br>
$yourFileName = 'myfile';
$ourFileHandle= fopen($path_to_file.$yourFileName, 'w') or die('Permission error');
I am trying to dynamically edit and update my /etc/hosts using the below php code
$string_Data = 'It works success!!';
$file_To_Edit = "/etc/hosts";
exec("sudo chmod 777 /etc/hosts");
$opened_File = file($file_To_Edit) or die("Error. Code:2 - Can not open file $file_To_Edit");
$lines = preg_grep(' /test.com/', $opened_File);
foreach(array_keys($lines) as $key) {
$opened_File[$key] = substr($opened_File[$key], 0, -1) . $string_Data;
}
$f = fopen($file_To_Edit, 'w') or die("Error $file_To_Edit");
fwrite($f, implode("\n", $opened_File));
fclose($f);
Can anyone tell me where am i going wrong, since i am not able to update the /etc/hosts file.
//suppose you are in /var/www/html/website/
$currentdir=getcwd(); // it will save your current directory location
chdir('../../../../etc/'); // it will change your web directory( /var/www/html/website/) to etc directory (/etc/)
$file='hosts';
$current=file_get_contents($file);
$string_Data = 'It works success!!';
$current.=$string_Data;
if(file_put_contents($file, $current))
{
echo "success in writing";
}
else
{
echo "fail in writing";
}
chdir("$currentdir"); // it will change directory (/etc/) to your web directory( /var/www/html/website/ )
//change permission before run php page because if your user don't have permission this code can't do writing
You need root rights to modify /etc/hosts file
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
}
My folder structure is like -
root
admin
create_page.php
pages
my_page1.php
my_page2.php
I have code for creating a new php file in "pages" folder. the code is like -
$dir_path = "../pages/";
$ourFileName = '../'.$page_name.".txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$ourFileContent = '<?php echo "something..." ?>';
if (fwrite($ourFileHandle, $ourFileContent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
The code executes normally..no problem. but the page is not being created. please tell me what i am doing wrong. is there problem with the path?
fclose($ourFileHandle);
Here's an example using the more simpler file_put_contents() wrapper for fopen,fwrite,fclose
<?php
error_reporting(E_ALL);
$pagename = 'my_page1';
$newFileName = './pages/'.$pagename.".php";
$newFileContent = '<?php echo "something..."; ?>';
if (file_put_contents($newFileName, $newFileContent) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "Cannot create file (" . basename($newFileName) . ")";
}
?>
$ourFileHandle = fopen("../pages/" .$ourFileName, 'w') or die("can't open file");
Make the above change to the third line and it will probably work; I tried it and it worked.
have you deliberately missed out on concatinating $dir_path and $ourFileName;
now
is your directory/file writable?
check for your current working directory
is your error_reporting on ?
Had the same Problem, File will not be created in a folder having different permission, make sure the permissions of the folders are same like other files.
Try this
$pagename = 'your_filename';
$newFileName = '../pages/'.$pagename.".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