Hi i am reading a file in php from folder admin filename is createfile.php
and thencreating new file its working fine but when i crete new file its create in same folder admin please help to to soleve this problem how can i crete file out side folder or in root place.
File structure
Admin--
|
|--createpage.php
Coode here:
creatpage.php
$filename="demo";
$fs=fopen("createfile.php", "r");
$ft=fopen("$filename", "w");
if ($fs==NULL)
{
echo "Can't Open Source File ...";
exit(0);
}
if ($ft==NULL)
{
echo "Can't Open Destination File ...";
fclose ($fs);
exit(1);
}
else
{
while ($ch=fgets($fs))
fputs($ft, $ch);
fclose ($fs);
fclose ($ft);
}
Please any one can help me Thanks
You can create a new file in your root path like the below:
$rootUrl = "your root url here";
$fs=fopen($rootUrl."createfile.php", "w");
Or if you want to create the file just outside Admin directory, you can use "../" like:
$fs=fopen("../createfile.php", "w");
Related
I have a script with a mysql query which saves a file called invoice.xml every day automatically by running a cron job. In case no data is found a no_orders.txt is saved.
I would like this file not be saved to the same folder as the script.php file is in but to a subfolder called invoices.
The renaming of the old invoice.xml is done with the following code
// rename old file
$nowshort = date("Y-m-d");
if(file_exists('invoice.xml')) {
rename('invoice.xml','invoice_'.$nowshort.'.xml');
}
The saving is done with the following code:
if($xml1 !='') {
$File = "invoice.xml";
$Handle = fopen($File, 'w');
fwrite($Handle, $xml1);
print "Data Written - ".$nowMysql;
fclose($Handle);
#print $xml;
die();
} else {
print "No new orders - ".$nowMysql;
$File = "no_orders_".$nowshort.".txt";
$Handle = fopen($File, 'w');
fclose($Handle);
die();
}
Could I please get assistance how to save this file to a subfolder. Also the renaming of the existing file would need to be within the subfolder then. I have already tried with possibilities like ../invoice/invoice.xml but unfortunately without any success.
Thank you
Just give the path of file 'invoice.xml' to $File.
Otherwise create some $Dir object which will point to Folder named 'invoice', then use accordingly
Use __DIR__ magic constant to retrieve your script.php directory, then you can append /invoice/invoice.xml .
Example if path to your script php something like this:
/var/www/path/to/script.php
$currentDir = __DIR__; //this wil return /var/www/path/to
$invoicePath = $currentDir.'/invoice/invoice.xml';
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.
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
}
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