PHP parse_ini_file work on URL? - php

Does the PHP method parse_ini_file work on an INI file hosted in the cloud? Right now, I have a config file that sits in every single one of my App servers, which could be anywhere from 4-8 at any given time. Making a config change is brutally painful to do by hand for each server.
I've tried the following but to no avail:
$handle = fopen('https://blah.com/config.ini', 'r');
$ini = parse_ini_file($handle, true);
but I get this error:
Warning: parse_ini_file() expects parameter 1 to be a valid path, resource given
Is this even possible? Any ideas?

fopen() is not used like that. Try file_get_contents():
$content = file_get_contents('https://blah.com/config.ini');
$ini = parse_ini_string($content, true);
This may work instead (not tested):
$ini = parse_ini_file('https://blah.com/config.ini', true);
In either case allow_url_fopen needs to be enabled in php.ini.

Fopen returns a pointer to the file, and parse_ini_file accepts file path and opens the file itself.
You should try and use file_get_contents() to get data from the file, which you then pass to parse_ini_string() function which is the same as parse_ini_file() but it takes ini file in a string.

Related

Laravel Filesystem - Move file from one disk to another

I am trying to move files from my FTP server to a local directory. First I need to find the correct file from the FTP server, as there can be a few hundreds:
//Search for the file.
$fileName= array_filter(Storage::disk('ftp')->files(), function ($file)
{
return preg_match('/('.date("Y-m-d" ,time()).').*.XLSX/', $file);
});
Above finds the correct file. If I dd($fileName), I get this:
My File Name.XLSX
I then try to move that file, to my public disk:
$ftp_file = Storage::disk('ftp')->get($fileName);
$local_file = Storage::disk('public')->move($ftp_file, "moved_file.xlsx");
However above code doesn't work. I get below error:
preg_match() expects parameter 2 to be string, array given
Which I have identified being on below function:
$ftp_file = Storage::disk('ftp')->get($fileName);
What am I doing wrong? How can I move the file, which I am able to find on my FTP server, to my local disk?
Thank you in advance.
As #Loek pointed out, $fileName was an array, and therefore to access it I needed to use:
$ftp_file = Storage::disk('ftp')->get($fileName[0]);

How to pass php vairables to a fopen

I'm struggling to use a php function, fopen with multiple variables.
I have two variables: $language is the extension ( E.G. .php ) and $url is a random number generated at the start of the script.
Here is my code but it always throws the die statement and doesn't work
$filename = "tools/scripts/tool".$language."?id=".$url;
$fh = fopen($filename, "w") or die("There Was An Error With The Script.");
Thanks
fopen will open the files content itself. It doesn't parse the file.
You might use exec() or you can call the file over a webserver.
fopen('http://localhost/yourfile.php?param='.$param);
Not to mention, that you are trying to write to a file there... ,"w")
yourfile.php?param=123 <- is not a valid filename

Include error in writing html file from php

I seem to have some problem with my code here. It creates a file from the php file, but I get an error on the include path.
include('../include/config.php');
$name = ($_GET['createname']) ? $_GET['createname'] : $_POST['createname'];
function buildhtml($strphpfile, $strhtmlfile) {
ob_start();
include($strphpfile);
$data = ob_get_contents();
$fp = fopen ($strhtmlfile, "w");
fwrite($fp, $data);
fclose($fp);
ob_end_clean();
}
buildhtml('portfolio.php?name='.$name, "../gallery/".$name.".html");
The problem seems to be here:
'portfolio.php?name='.$name
Any way I can replace this, and still send the variable over?
Here's the error I get when I put ?name after the php extension:
Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Warning: include() [function.include]: Failed opening 'portfolio.php?name=hyundai' for inclusion (include_path='.;C:\php\pear') in D:\Projects\Metro Web\Coding\admin\create.php on line 15
Now I saw your code in the comment to a previous answer I'd like to point few things out
function buildhtml($strhtmlfile) {
ob_start(); // redundant
$fp = fopen ($strhtmlfile, "w"); // redundant
file_put_contents($strhtmlfile,
file_get_contents("http://host/portfolio.php?name={$name}"));
// where does $name come from?? ---^
close($fp); // also redundant
ob_end_clean(); // also redundant
}
buildhtml('../gallery/'.$name.'.html');
In PHP as in many other languages you can do things in different ways. What you've done is you took three different ways and followed only one (which is absolutely enough). So when you use functions file_put_contents() and file_get_contents() you don't need the buffer, that is the ob_ family of functions, because you never read anything in the buffer which you should then get with ob_get_contents(). Nor you need the file handles created and used by fopen(), fclose(), because you've never written to or read from the file handle i.e. with fwrite() or fread().
If I'm guessing correctly that the purpose of your function is to copy html pages to local files, my proposal would be the following:
function buildhtml($dest_path, $name) {
file_put_contents($dest_path,
file_get_contents("http://host/portfolio.php?name={$name}"));
}
buildhtml('../gallery/'.$name.'.html', $name);
file_put_contents($strhtmlfile, file_get_contents("http://host/portfolio.php?name={$name}"))
Is it ok?
The output of:
'portfolio.php?name='.$name, "../gallery/".$name.".html";
is:
portfolio.php?name=[your name]../gallery/[your name].html
Are you sure that's what you want ?
include/require statements in PHP allow you to access the code contained in a file which is already stored on the server
What you are trying to achieve is including the output result of executing the code in that file with specific parameters
The suggested example offered by MrSil allows you to request the execution of the code in those files and offer parameters. The reason it shows you a blank page is because file_put_contents 'saves data to a file' and file_get_contents does not echo the result, it returns it. Remove the file_put_contents call, and add an echo at the beginning of the line before file_get_contents and it should work.
echo file_get_contents('http://domain.com/file.php?param=1');
As a warning this approach forces the execution of 2 separate PHP processes. An include would have executed the code of the second file within the first process.
To make the include approach work you need to include the file as you first did but without specifying parameters. Before including each file you need to setup the parameters it is expecting such as $_GET['name'] = $name

Fopen to a remote php script

i have to download a file created from a php script.
i tried this:
fopen('www.example.com/download.php?key=value', 'rb');
but i stille get a "failed to open stream" error.
how can i do that? If I browse to the url i get the file without problems...
EDIT: sorry, i forgot a piece of the string :)
I see multiple issues with you request:
You need to specify the open mode. In your case only 'r' applies because you only want to read.
You need to specify the protocol. In your case "http".
You need to have URL wrappers enabled. Do a phpinfo() and look if allow_url_fopen is set to On.
You probably wanted file_get_contents anyway.
You should enable error reporting and read the error messages. That will help you track the problem faster.
Don't forget to fclose if you decide to use fopen.
Example:
$data = file_get_contents('http://www.example.com/download.php?key=value');
You should also read about fopen in the Manual.
It looks like you need to read the usage for fopen first, here is an example usage:
fopen ("http://www.example.com/", "r");
r = read, that might be causing your failure.

file_get_contents with HTTP: No such file or directory

I'm trying to execute a straightforward PHP call to load the contents of a web page:
$result = file_get_contents("http://www.google.com");
The result coming back is a strange file not found error:
Warning: file_get_contents(http://www.google.com): failed to open stream: No such file or directory in /var/www/html/test.php on line 5
I have "allow_url_fopen = On" on my php.ini and no .htaccess files that might alter the setting in the directory. Any ideas? I've never had trouble with this function before on different servers.
Apparently the HTTP stream wrapper is not present, which causes this error.
print_r(stream_get_wrappers());
Array
(
[0] => php
[1] => file
[2] => data
[3] => compress.zlib
)
I'm not sure how it was removed or how to restore it, but that would explain it! I've tried stream_wrapper_restore('http') in case it was unregistered somehow, but that has no effect.
I think it is some kind of proxy effect.
Do you use proxy? If this is the case, you must create a stream context with the proxy details.
Did you re-initialize your webserver on changing "allow_url_fopen"?
OR
The user agent "PHP" may be disallowed on the server you are querying.
OR
From the PHP Manual page: Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().
You can do a test of the php.ini settings like this...
if (ini_get('allow_url_fopen') == '1') {
// use fopen() or file_get_contents()
} else {
// use curl or your custom function
}
Not sure, but try:
if(($fp=fopen('http://www.google.com/', 'rb'))!=null)
{
// for php5 and up or use fread for php4
$contents = stream_get_contents($fp);
fclose($fp);
}

Categories