php query output save as text file

回眸只為那壹抹淺笑 提交于 2019-12-07 01:55:29

Please consider using file_put_contents(). Also verify that you have write privileges in the server. You can do this by using is_writable().

But this just saves the page as text including the php. -> Do you want the server to process savechoice.php? This is totally different. When you open files using file_get_contents(), it gets the file exactly how you see it when you open the file. It will not execute the file.

You need to change how savechoice.php works. If it's a script that has some output, you can instead put the output in a variable which you will place in your $data. For example:

savechoice.php

<?php
echo 'Hello World!';
?>

Change that to something like this:

<?php
$echo = 'Hello World!';
?>

In your other file, you can simply use file_put_contents($echo); after calling savechoice.php using require().

You'd probably want something more like this:

ob_start();
include('savechoice.php');
$text = ob_get_clean();

but really, the best method is to modify your code in savechoice.php to be callable as a function, so you can have more like:

$text = get_choice(...);

instead, which the appropriate logic to output ONLY the data you want, without the html wrapping paper. Invoking HTTP requests to your own server is a pointless waste of resources in almost all usage cases, especially when you're using sessions (and particularly standard file-based sessions).

If you want to get the output of the file instead of the raw contents, you need to make sure that the file is executed as php.

You can include it (I see there already is another answer...) or you can use:

$url = "http://localhost/path/to/file/savechoice.php";

with your original code to make sure the file gets processed by the web-server.

That is because you are loading the file, not the url of the file.

Suppose your site's domain is http://www.example.com and that the file "savechoice.php" is in the web root. If you change the $url to

$url = "http://www.example.com/savechoice.php";

The php in the file will then be interpreted and you will see the web page and not the php.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!