php的查询输出保存为文本文件 [英] php query output save as text file

查看:177
本文介绍了php的查询输出保存为文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上有一个页面,用于从表格中提取信息并用css格式化。



我无法找到一种方法将页面另存为是作为文本或pdf文件。我找到了一个可以执行此 http://pdfmyurl.com/ 的pdf网站,但我希望能够做同样的事情我的网站没有外部网站。



网页有一个会话,确保用户登录,并且取决于 user_id 取决于将数据拉出数据库。为了澄清,我希望用户能够查看此页面。在页面上,我希望有一个保存到计算机按钮,可以按原样保存该页面。



我发现

 <?php 

//链接下载文件...
$ url =savechoice.php;

//获取文件的代码...
$ data = file_get_contents($ url);

//另存为?
$ filename =test.txt;

//保存文件...
$ fh = fopen($ filename,w);
fwrite($ fh,$ data);
fclose($ fh);

//显示您刚保存的文件的链接...
echo< a href ='。$ filename。'>点击此处< / a>下载文件...;
?>

但这只是将页面保存为文本,包括未执行的php。
我也将网址更改为完整的网址,尽管运行PHP会返回空白,因为会话无法识别。

解决方案

请考虑使用 file_put_contents()。同时验证您是否拥有服务器的写入权限。您可以使用 is_writable()来完成此操作。



但这只是将页面保存为包含php的文本。 - >您希望服务器处理 savechoice.php 吗?这完全不同。当您使用 file_get_contents()打开文件时,它会在您打开文件时准确获取文件的显示方式。它会不是执行文件。



您需要更改 savechoice.php 作品。如果它是一个具有一些输出的脚本,则可以将输出放在一个变量中,该变量将放置在 $ data 中。例如:

savechoice.php

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

将其改为如下所示:

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

在其他文件中,您可以简单地使用 file_put_contents($ echo); 使用 require()调用 savechoice.php


I have a page on my website that pulls out information from a table and is formatted with css.

I can't find a way to save the page as is, as a text or pdf file. I found a pdf site that does this http://pdfmyurl.com/ but I was hoping to do the same thing on my own site wihtout external sites.

The web page has a session making sure the user is logged in, and depending on the user_id depends what data is pulled out the database. To clarify, I want the users to be able to view this page. On the page I was hoping for a "SAVE TO COMPUTER" button that would save that page as is.

I found

<?php

 //Link to download file...
 $url = "savechoice.php";

 //Code to get the file...
 $data = file_get_contents($url);

 //save as?
 $filename = "test.txt";

 //save the file...
 $fh = fopen($filename,"w");
 fwrite($fh,$data);
 fclose($fh);

 //display link to the file you just saved...
 echo "<a href='".$filename."'>Click Here</a> to download the file...";
?>

But this just saves the page as text including the php not executed. I also changed the url to the full url and although running the PHP comes back blank as the session isn't recognised.

解决方案

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().

这篇关于php的查询输出保存为文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆