file_get_contents doesn't work [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
$z = file_get_contents("vars.txt");
var_dump($z);
This is my current code. When I run it I see this output:
string(0) ""
I am not sure why - this file contains 321 characters. It is in the same directory with my PHP script. Can you explain me reasons, why that doesn't work? Writing to this file works ok (with "w+" mode).
I though this is chmod mistake, but I wrote chmod 777 dir command, and still doesn't work.
Full code of my script is there: https://gist.github.com/ty221/274bda4ecec03f710691
Displaying all errors E_ALL doesn't work - I mean nothing is getting displayed.

How about reading the manual: http://php.net/manual/en/function.fopen.php
And a quote from there:
'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
^^^^^^^^^^^
That means with your line:
//Deletes all content in the file
$file = fopen("vars.txt", "w+");
$to_save = "";
//Reads the content from a empty file
$z = file_get_contents("vars.txt");
//Outputs an empty string
var_dump($z);

Related

mkdir() returns No such file or directory in <b>...</b> on line <b>..</b><br /> [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm PHP developer but i cant understand this error
$uid = $this->db->Tables("telegrambots")->search([
"telegrambotid" => $this->botKey
])['uniqueId'];
if (!file_exists("TelegramBotCommands/{$uid}"))
mkdir("TelegramBotCommands/{$uid}");
Eval is evil, you probably don't need it so don't use it. You want to make a call to a class with a dynamic name? Use this:
$dynamic_class_name = 'Video';
$video = new $dynamic_class_name();
That being said, your snippet with eval seems to work perfectly fine:
http://sandbox.onlinephpfunctions.com/code/e3bb43b1ccfd27365247120e9c5751aac9e2b4ce
You would have to check your logs as to what the error is.
EDIT:
As you said you are using namespaces, try to use the full classname including the namespace in the eval function (like new \namespace\Videos(.... Even better though: don't use eval!

File_get_contents doesn't import php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this code:
elseif($pagina == "agenda")
{
$html = file_get_contents("template/index.php");
$html = str_replace("%content%",file_get_contents("template/agenda.php"),$html);
$html = str_replace("%current2%",'class="current"',$html);
$html = str_replace("%sidebar%",file_get_contents("template/sidebar/sidebar_nieuws.php"),$html);
}
On the page "agenda.php" is a normal php script. This does work when I open agenda.php directly, but when I open it with this code, the code doesn't work. I think file_get_contents doesn't work on php? What is an alternative?
(Sorry for my bad english ;)..)
I would use output buffering. It's effective in this kind of scenario.
$html = file_get_contents("template/index.php");
$arr = array();
ob_start();
include_once('template/agenda.php');
$file = ob_get_contents(); //$file now stores the rendered output (after PHP has run) of template/agenda.php
ob_end_clean();
$arr['agenda'] = $file;
$html = str_replace("%content%", $arr['agenda'], $html);
if you want to run the php code. Then you can include your php file:
include('path/to/file.php');
if you want to change php code from file before executing then in the end run eval($html);

Why convert IP Address to Hostname by used "gethostbyaddr" but don't work? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I try to convert IP Address to Hostname by used "gethostbyaddr" but don't work. I use file.text that is log file to analyze which page that user used a lot or less. So I try to cut sentence that use array. This is my code.
$file=fopen("log.txt","r");
while (!feof($file)) {
$buffer = fgets($file, 4096);
$text= explode(" ",$buffer,10);
$text_2=#$text[1];
$ip=substr($text_2,1,11);
$ip2=gethostbyaddr($ip);
echo"INSERT INTO `log` VALUES ('$ip2');"."<br>"; //Show results
//echo"<br>"; //New line
}
fclose($file);
Thanks
that means gethostbyaddr failed.
not because your input, find the explanation in php documentation
http://php.net/manual/en/function.gethostbyaddr.php
"Returns the host name on success, the unmodified ip_address on failure, or FALSE on malformed input."

simplexml_load_file() with a variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm currently having an issue with simplexml_load_file(); my xml path is a url, that is rendered as a variable
$xurl = "domain/pathto/myfile.xml"; // This is actually a variable that returns the entire URL to where my xml file is -- this will change from file to file
$xmlpath = parse_url($xurl, PHP_URL_PATH); // to get the path of my xml file ex. /pathto/myfile.xml
$xmlpath = mb_substr($xmlpath, 1); // returns pathto/myfile.xml
here is where my problem is, when I put it into :
simplexml_load_file($xmlpath);
In my function, I get nothing appearing from the XML file
However, in my same function if I change it to
simplexml_load_file("pathto/myfile.xml");
My function works fine.
I did an echo on $xmlpath and it returns the pathto/myfile.xml just fine.
<?php echo $xmlpath; ?> // returns pathto/myfile.xml
What am I doing wrong?
EDIT: Phil
echo strcmp("pathto/myfile.xml", $xmlpath)
returns a 0.

Adding to .txt file in PHP instead of overwriting it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this line of PHP code here
<?php
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
?>
Whenever
phpfile.php?blablabla
is queried it writes a query.txt with the parameter in it, in this case, blablabla.
BUT, when I do this, it deletes the past query.txt file and writes a totally new one.
I want the queries to be ADDED into the .txt file. So there can be as many queries and possible and every single value entered will be lined up in the .txt file..
For example
I want it so phpfile.php?test is visited, query.txt looks like this
test
after that, phpfile.php?test2 is visited
now query.txt looks like this
test
test2
and this goes on forever.
How do I do this?
p.s.: sorry, I'm totally a Java type. I'm an absolute beginner to PHP.
You wish to append to the file, so you should use something along the lines of
$handle = fopen("myfile.txt", "a");
fwrite ($handle, parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY));
fclose($handle);
You can find all the other read/write file open flags at the docs.
http://php.net/manual/en/function.fopen.php
It appears you can also just pass a flag with your existing code, as that will abstract the file handle open/close from you;
file_put_contents('query.txt', parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), FILE_APPEND);
Add FILE_APPEND as 3rd argument to file_put_contents(). Example (.PHP_EOL to add new line):
$data = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY).PHP_EOL;
file_put_contents('query.txt', $data, FILE_APPEND);

Categories