Is it possible to load a php file in codeigniter dynamically? - php

I want to load a php file dynamically. for example i have a website and i added new php file and it will load the new php file. is it possible without going through controller?

Try this, if you are not using cURL.
file_get_contents('http://YOUR_CODE_URL.com/send.php?'.$getdata, false, $context);
Furthermore, the method defaults to GET so you don't even need to set options, nor create a stream context. So, for this particular situation, you could simply call file_get_contents with the first parameter if you wish.

Related

DOMDocument->loadHTMLFile() I/O warning : failed to load external entity, only fails when called by ajax

I have a function which creates a new instance of a form template I've made (not using a template engine) and fills it in with various data based on an object thats passed to it. I use this function to create forms for existing objects when the page is loaded, but I also have a button in the interface which makes an ajax request to a separate script which creates a new object and then calls that function to create a form for the object to be sent as a response to the ajax.
The error is:
Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "templates/form_edit_event.html" in C:\wamp64\www\<private>\src\<private>\components\component_edit_event.php on line 12
The code it references:
11 $eventDom = new DOMDocument();
12 $eventDom->loadHTMLFile('templates/form_edit_event.html');
The thing is though, when the page is first loaded, my script and the loadHTMLFile function work just fine to create the forms for the already existing objects. The I/O error only occurs when I use the button to try to make a new one. The exact text of which is as follows, although I obscured some directory names that shouldn't matter:
The files it's loading is pure html, not even a header/footer/etc, just <form>contents</form>. I have no idea why it would fail only sometimes.
The entire script (minus some irrelevent use/includes) being called from ajax is this, with component_edit_event.php being the script mentioned above which creates the form:
include_once __DIR__ . '\..\components\component_edit_event.php';
$tourneyId = $_POST['tourneyId'];
/** #var DimTournament $tourney */
$tourney = DimTournament::findById($tourneyId);
$newEvent = new DimEvent($tourney);
echo createEventForm($newEvent)->saveHTML();
I don't think I should need to include any other code but I absolutely can if needed.
Swapping the relative path in $eventDom->loadHTMLFile('templates/form_edit_event.html'); to an absolute path fixed the issue. The script that was throwing the error was in a different directory from the script which was working fine.
I'll be making a config file with some sort of $srcRoot variable for the sake of keeping some semblance of pseudo-relativity in the path names.

file_get_contents not working when parameters included in a relative url

I am trying to create a simple web service that will give a result depending on parameters passed.
I would like to use file_get_contents but am having difficulties getting it to work. I have researched many of the other questions relating to the file_get_contents issues but none have been exactly the situation I seem to having.
I have a webpage:
example.com/xdirectory/index.php
I am attempting to get the value of the output of that page using:
file_get_contents(urlencode('https://www.example.com/xdirectory/index.php'));*
That does not work due to some issue with the https. Since the requesting page and the target are both on the same server I try again with a relative path:
file_get_contents(urlencode('../xdirectory/index.php'));
That does work and retrieves the html output of the page as expected.
Now if I try:
file_get_contents(urlencode('../xdirectory/index.php?id=100'));
The html output is (should be): Hello World.
The result retrieved by the command is blank. I check the error log and have an error:
[Fri Dec 04 12:22:54 2015] [error] [client 10.50.0.12] PHP Warning: file_get_contents(../xdirectory/index.php?id=100): failed to open stream: No such file or directory in /var/www/html/inventory/index.php on line 40, referer: https://www.example.com/inventory/index.php
The php.ini has these set:
allow_url_fopen, On local and On master
allow_url_include, On local and On master
Since I can get the content properly using only the url and NOT when using it with parameters I'm guessing that there is an issue with parameters and file_get_contents. I cannot find any notice against using parameters in the documentation so am at a loss and asking for your help.
Additional Notes:
I have tried this using urlencode and not using urlencode. Also, I am not trying to retrieve a file but dynamically created html output depending on parameters passed (just as much of the html output at index.php is dynamically created).
** There are several folks giving me all kind of good suggestions and it has been suggested that I must use the full blown absolute path. I just completed an experiment using file_get_contents to get http://www.duckduckgo.com, that worked, and then with a urlencoded parameter (http://www.duckduckgo.com/?q=php+is+cool)... that worked too.
It was when I tried the secure side of things, https://www.duckduckgo.com that it failed, and, with the same error message in the log as I have been receiving with my other queries.
So, now I have a refined question and I may need to update the question title to reflect it.
Does anyone know how to get a parameterized relative url to work with file_get_contents? (i.e. 'file_get_contents(urlencode('../xdirectory/index.php?id=' . urlencode('100'))); )
Unless you provide a full-blown absolute protocol://host/path-type url to file_get_contents, it WILL assume you're dealing with a local filesystem path.
That means your urlencode() version is wrongly doing
file_get_contents('..%2Fxdirectory%2Findex.php');
and you are HIGHLY unlikely to have a hidden file named ..%2Fetc....
call url with domain, try this
file_get_contents('https://www.example.com/inventory/index.php?id=100');
From reading your comments and additional notes, I think you don't want file_get_contents but you want include.
see How to execute and get content of a .php file in a variable?
Several of these answers give you useful pointers on what it looks like you're trying to achieve.
file_get_contents will return the contents of a file rather than the output of a file, unless it's a URL, but as you seem to have other issues with passing the URI absolutely....
So; you can construct something like:
$_GET['id'] = 100;
//this will pass the variable into the index.php file to use as if it was
// a GET value passed in the URI.
$output = include $_SERVER['DOCUMENT_ROOT']."/file/address/index.php";
unset($_GET['id']);
//$output holds the HTML code as a string,
The above feels hacky trying to incorporate $_GET values into the index.php page, but if you can edit the index.php page you can use plain PHP passed values and also get the output returned with a specific return $output; statement at the end of the included file.
It has been two years since I used PHP so I am just speculating about what I might try in your situation.
Instead of trying fetching the parsed file contents with arguments as a query string, I might try to set the variables directly within the php script and then include it (that is if the framework you use allows this).
To achive this I would use pattern:
ob_start -> set the variable, include the file that uses the variable -> ob_get_contents -> ob_end_clean
It is like opening your terminal and running the php file with arguments.
Anyway, I would not be surprised if there are better ways to achieve the same results. Happy hacking :o)
EDIT:
I like to emphasize that I am just speculating. I don't know if there are any security issues with this approach. You could of course ask and see if anyone knows here on stackoverflow.
EDIT2:
Hmm, scrap what I said last. I would check if you can use argv instead.
'argv' Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string. http://php.net/manual/en/reserved.variables.server.php
Then you just call your php script locally but without the query mark indicator "?". This way you can use the php interpreter without the server.
This is likely to be the most general solution because you can also use argv for get requests if I am understanding the manual correctly.

jQuery load php file as text?

Is it possible to load a php file as text with jquery?
$('#loader').load('somefile.php', function(e){
console.log(e);
});
This always interprets/execute the php file but I'm looking for a way to only load it as text, without to resort to renaming my php file as .txt
Is it possible?
Cheers
It is not possible without making any server side modification. The web server will always interpret the php file and return the output. However does not matter what solution you find it'll be very dangereous since you'll be dumping content of your php file to public.
Possible solutions with server side modifications:
Create a PHP file that dumps the content of a file, which name is specified by a url argument
Rename the file (I know the op does not want this, just included since it's an option)
As #nicholas-young suggested, get rid of the PHP tags.
I'm not sure why you need this type of need but I want to emphasize that this might not be a good idea in most of the cases since you'll be make a working PHP file available to public. If you can explain more why you need this we might offer better solutions.
Update:
Create a dumper.php that requires authorization and call this file from the javascript side with passing the filename that you want to be dumped as a parameter (dumper.php?file=index.php)
echo file_get_contents($_GET['file']);
It is of course not possibile.
.load will make an HTTP request to yourwebsite.com/somefile.php hence you will obtain the result of your script not the PHP code inside it.
If you really need the raw code inside your javascript as a string you should output it from the php itself:
<script>
var yourCode = <?=json_encode(file_get_contents('somefile.php')) ?>;
</script>
NO! Would be a major security problem if possible. The header will not matter. If making request towards php file, it will execute prior to delivery.
Use some parameter to print out contents from file instead. But do it in the file itself.

How to integrate a new file in a module in SugarCRM 7?

I added a new button in detailviews of a module, having the attribute onClick="genReg(name, type)".
I created this function in a .js file. This function (genReg(name, type)) send the variables to a PHP file, generateReg.php, using JSON.
The file is in *module_name* folder. In this PHP file I try to create a new object of modules Reglement (it's a custom module) and add new item to the database.
The problem is that it doesn't recognize the path to the module Reglement.
I included it as follows:
require_once('modules/Reglement/Reglement.php');
but got the following error:
Error: Failed opening required 'modules/Reglement/Reglement.php'
Paths to my files:
js: custom/module_name/js/generate.js
php: custom/module_name/generateReg.php
Which file shall I include in order to recognize all the path?
Yes, I did that error :D, calling directly. I change the url in js file like this http://localhost.net/index.php?entryPoint=generateReg and it works now. Thank a lot.
But I've got an other error. When I call this url I send POST data. In my file I have the POST data, but the function json_decode($_POST['data']) return nothing.
print_r($_POST['data']) -> return a valid json
json_decode($_POST['data']) return nothing
Do you know if Sugar do something that change the behaviour of json_decode?

custom php function creation and install

I would like to know how to create a php function that can be installed in php
just like the already built in functions like :
rename
copy
The main point I would like to achieve is a simple php function that can be called from ANY php page on the whole host without needing to have a php function within the php page / needing an include.
so simply I would like to create a function that will work like this :
location();
That without a given input string will output the current location of the file via echo etc
Well, there are a couple of options here. One of them is to actually extend the language by writing an extension. You'd have to muck around with the PHP source code, write it in C, and deal with the Zend Engine internally. You probably wouldn't be able to use this on a shared host and it would be quite time consuming and probably not worth it.
What I would do is put all of your functions into a separate PHP file, say helper_functions.php. Now, go into your php.ini and add the directive: auto_prepend_file = helper_functions.php. This file should be in one of the directories specified in your include_path (that's a php.ini directive too).
What this does is basically automatically put include 'helper_functions.php'; on every script. Each and every request will have these functions included, and you can use them globally.
Read more about auto_append_file.
As others have said, there's probably an easier, better way to do most things. But if you want to write an extension, try these links:
http://docstore.mik.ua/orelly/webprog/php/ch14_01.htm
http://www.tuxradar.com/practicalphp/2/3/0
So you want to extend PHP's core language to create a function called location(), written in C, which could be done in PHP by:
echo __FILE__;
Right. Have fun doing that.

Categories