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);
Related
So I'm basically creating .txt files with unique filenames and then changing the ext. of the file to .mobileconfig. That's the overall goal but I wanted the files in a different directory, that's not my root directory.
So it's basically an HTML form, then it takes the data submitted through that form, figures out what to do with it here:
<?php
$txt = $_POST['content'];
$UUID = $_POST['UUID'];
$genfile = fopen('./generated/'$UUID.'.txt', w);
file_put_contents('./generated/'$UUID.'.txt', $txt);
rename('./generated/'$UUID.'.txt', './generated/'$UUID.'.mobileconfig');
?>
I had it working, but it was in the same directory as my other files. I've tried the code above, I've tried it using " instead of '. I've tried without the period before the /, and I've tried without the ./ all together.
Is there anything else that I could try besides just moving the .php file to another directory because I do want to set something up where it deletes all the files inside the generated folder every x amount of time.
You have many syntax errors in your code.
Additionally, using file_put_contents() doesn't require that you open the file first.
Lastly, why create a file with a .TXT extension if you're going to rename it immediately afterwards?
Try this:
$txt = $_POST['content'];
$UUID = $_POST['UUID'];
$fname = "./generated/$UUID.mobileconfig";
file_put_contents($fname, $txt);
See the PHP manual for details on variable expansion in double-quoted strings
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);
So in my script I copy a PHP file used as a model and/or template file. But i can not figure out how to find the data that needs to be edited, and editing it. I don't want to truncate the whole file just need to edit a line of text in the file and save it. Below is what I have so far.
$file = '../postback/postback.php';
$pbac = '../postback/'.md5($affn).'.php';
copy($file, $pbac);
$files = file_get_contents($pbac);
// Stick the new IP just before the closing </files>
$new_files = str_replace('//ADD NAME HERE//', "//ADD NAME HERE//\n\$name ='".$affn."';", $files);
// And write the new string back to the file
file_put_contents($pbac, $new_files);
$pback = '/postback/'.md5($affn).'.php';
As you can see I have the code to do it. But it is not working. I don't know what i am doing wrong. perhaps someone else looking at it can point out the problem.
The file is copied.
There are no errors displaying or listing in my error log.
There are no edits being made though.
//ADD NAME HERE//
Is on a line by itself.
A semi-colon was missing for the $affn variable.
What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)
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.