Is this possible to store other php code inside a variable.
I am trying to create php page from backend as same in CMS
$var = include 'page.php' . "
php code start
$code = include 'bootstrap.php' . "
// header code
php code
}
// footer code
";
$menu_alias = "newHTMLFileName";
$path= "../folder".$menu_alias.".php";
if (!file_exists ($path))
{
$handle = fopen ($path, "w");
$write = fwrite ($handle, $code);
fclose($handle);
echo "created";
}
else
{
echo "already created";
}
I want to know if it is this possible to write PHP code and MySQL query and store both to a variable.
Thanks in advance
Related
I'm a beginner so sorry if my question is inappropriate.
I'm trying to create a function that allows me to load and display a csv file in PHP, but it keeps giving me an error.
Here is the function:
<?php
function load_csv_file($nom){
$tableau_asso= array();
$fichier = fopen($nom, "r");
while($ligne = fgetcsv($fichier, 1024, ';')){
array_push($tableau_asso, $ligne);
}
fclose($fichier);
foreach($tableau_asso as $ligne){
print($ligne[0]);
print(", ");
print($ligne[1]);
print("<br>");
}
return $tableau_asso;
}
?>
Then I created another document where I call this function:
<?php
include("library.php");
$nom = 'C:\\MAMP\\htdocs\\MEDAS-PHP\\data.csv';
$mon_tableau = load_csv_file($nom);
?>
When I try to load it, nothing happens. What am I doing wrong?
I have two scripts: one of them writes the value of a variable to a file. In another script, I try to read it. It is written without problems, but it is not readable.
Here I write to a file:
$peer_id=2000000001;
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
$file = fopen($fileLocation,"a+");
fwrite($file, $peer_id);
fclose($file);
Here I read the file:
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
$file = fopen($fileLocation,"r");
if(file_exists($fileLocation)){
// Result is TRUE
}
if(is_readable ($file)){
// Result is FALSE
}
// an empty variables, because the file is not readable
$peer_id = fread($file);
$peer_id = fileread($file);
$peer_id = file_get_contents($file);
fclose($file);
The code runs on "sprinthost" hosting, if that makes a difference. There are suspicions that this is because of that hosting.
file_get_contents in short runs the fopen, fread, and fclose. You don't use a pointer with it. You should just use:
$peer_id = file_get_contents($fileLocation);
That is the same for is_readable:
if(is_readable($fileLocation)){
// Result is FALSE
}
So full code should be something like:
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
if(file_exists($fileLocation) && is_readable($fileLocation)) {
$peer_id = file_get_contents($fileLocation);
} else {
echo 'Error message about file being inaccessible here';
}
The file_get_contents has an inverse function for writing; https://www.php.net/manual/en/function.file-put-contents.php. Use that with the append constant and you should have the same functionality your first code block had:
file_put_contents($fileLocation, $peer_id, FILE_APPEND | LOCK_EX);
I've been working o this for the last few weeks and can't find an alternate route. What I need to do is return the contents of a text file after the file has been read. I have two different logs that use text files to log errors. The first log returns the correct variable that I ask for but for some reason, even though I use the exact same methods to call the variable, it doesn't return anything. If I echo the variable then the correct string is displayed but the variable returns nothing. Here is the function:
function GetNoticeLog($strDate){
$logdate = preg_replace("/[^0-9]/", "_", $strDate );
$strFileName = realpath('debuglogs/enotice_logs').'/ENOTICELOG_' . $logdate . '.txt';
if(is_readable($strFileName)){
$file = fopen($strFileName,"r");
$contents = fread($file, filesize($strFileName));
$fclose($file);
return nl2br($contents);
}
else if(!is_readable($strFileName)){
echo $strFileName." is unreadable";
}
}
Why does this function return the necessary string when executed in one function but has to be echoed to see content in the other is my question.
Try changing
$fclose($file);
to this
fclose($file);
I was able to get the code to work on my server. The only change I made here is the path to the file which is in the same directory as the PHP script.
<?php
function GetNoticeLog($strDate){
$logdate = preg_replace("/[^0-9]/", "_", $strDate );
//$strFileName = realpath('debuglogs/enotice_logs').'/ENOTICELOG_' . $logdate . '.txt';
$strFileName = "blah.txt";
if(is_readable($strFileName)){
$file = fopen($strFileName,"r");
$contents = fread($file, filesize($strFileName));
fclose($file);
return nl2br($contents);
}
else if(!is_readable($strFileName)){
echo $strFileName." is unreadable";
}
}
$stuff = GetNoticeLog("whatever");
echo $stuff;
?>
I'm writing a simple text editor for a template, and I've gotten the opening, displaying, and editing part handled. Every time I try to save it though, it keeps giving me an error on the fopen() function.
I'm getting the files with this:
$dir = "./uploads/post-templates";
$files = scandir($dir);
while($files[0] == "." || $files[0] == "..") {
array_shift($files);
}
Then a simple loop handles displaying filenames in a select menu:
<?php foreach($files as $f) { echo "<option name='file' value=" . $f . " class='file'>" . $f . "</option>";}; ?>
Lastly it is all appended into the textarea using a short jQuery function. Alas, when it comes to executing the script to save the file, I get an error every single time. I've tried using relatives, absolutes, and http for the directory, and the filename and path are echoing properly each time.
///different file!!!!
$f = $_POST['file'];
$c = $_POST['content'];
$dir = "./uploads/post-templates/";
$file = $dir . $f;
echo $file;
$fo = fopen($file, "w") or die("opening error");
fwrite($fo, $c) or die("writing error");
fclose($f);
NOTE: For testing purposes only.
I wrote a test script and it was successful.
With the values that you have Mike, try using my script below with your present incoming values.
Plus this line gave me an error from your original code: fclose($f);
Error: when using fclose($f);
Warning: fclose() expects parameter 1 to be resource, string given in...
It should read as fclose($fo);
TEST CODE:
<?php
$f = "thefile.txt";
$c = "the content";
$dir = "./test/";
$file = $dir . "/" . $f;
echo $file; // echos the file name at this point
$fo = fopen($file, "w") or die("opening error");
fwrite($fo, $c) or die("writing error");
fclose($fo);
// shows the contents of the written file on screen
$contents = file_get_contents($file);
echo $contents;
?>
After checking for both fread and fopen with the search-command "php fread php code" and php fopen php code" without success I'm now turning to asking the question myself. (Over 300 pages with questions were a bit to steep to dig around in.)
I have a page where I get the content from external files. I got the index.php with the links which sends requests through the url (?links=home, for example) that is read from another file that looks through an array and finds the right file. All that works! But here is the tricky part:
On of the files includes a few strings of php-codes that won't do it's job but just hangs around in the view-source. Yes, you can see the commands in the source:code, but it won't anything I request. Not a single echo.
Here is some code that might explain things even better.
The code that gets the url-command:
<?php
function load_pages() {
if ($_GET['link'] != NULL) {
$link = $_GET['link'];
$links = array("hem" => "hem.php", "about" => "about.php", "blogg" => "blogg.php", "kontakta" => "kontakta.php");
foreach ($links as $key => $value) {
if ($key == $link) {
$file = "links/" . $value;
$fh = fopen($file, "r") or exit("Unable to open the file.");
$fileContent = fread($fh, filesize($file));
fclose($fh);
echo $fileContent;
}
}
} else {
$file = "links/hem.php";
$fh = fopen($file, "r") or exit("Unable to open the file.");
$fileContent = fread($fh, filesize($file));
fclose($fh);
echo $fileContent;
}
}
?>
The file that gets the command for the page I want to load:
<?php
include ("../include/functions.php");
connect();
?>
<h1>Blogg</h1>
<?php
if ($_GET['id'] == NULL) {
blogg_content();
} else {
blogg_link();
}
?>
<div id="blogg_menu">
<?php blogg_menu(); ?>
</div>
What comes out is: Blogg
That just doesn't do the trick, so what might I change to make it give me the blog-content and such? (The page is on Swedish, just to disclaim any typos about "Blogg".)
If those files contain PHP code that you want to be executed you need to include or require them rather than echoing the raw data.
Please see PHP's documentation on how to include code files.
EDIT
Kind of hard to tell from your description but if at all, you would have to do something like:
...
if ($key == $link) {
$file = "links/" . $value;
include_once $file;
}
...