简体   繁体   English

如何从我的服务器 [PHP] 读取 .txt 文件?

[英]How can I read .txt file from my server [PHP]?

I'm creating a Telegram Bot in PHP.我正在用 PHP 创建一个 Telegram Bot。 My aim now is read from a .txt file a integer and put him in a variable.我现在的目标是从 .txt 文件中读取一个整数并将他放入一个变量中。 This text.txt is uploaded in a server.此 text.txt 上传到服务器中。 I tryed to do this:我试图这样做:

$filename = "http://<MY HOST NAME>/test_bot/file/text.txt";
$fp = fopen($filename, "r+");
$send = fgets($fp);
fclose($fp);

echo $send;

But when I try to open my index.php, nothing comes out written.但是当我尝试打开我的 index.php 时,什么也没有写出来。 am I doing something wrong?难道我做错了什么?

Since you are opening from a remote host you can't open for read/write, just read.由于您是从远程主机打开的,因此无法打开进行读/写,只能读取。 Try this instead:试试这个:

$fp = fopen($filename, "r");

If you are trying to open a file on the same server as the PHP script then do not use the "http://..." path but instead the local file path and then you can open it for read/write.如果您尝试在与 PHP 脚本相同的服务器上打开文件,则不要使用“http://...”路径,而是使用本地文件路径,然后您可以打开它进行读/写。 In the case of your script it does not appear you need write access so "r" should be sufficient.对于您的脚本,您似乎不需要写访问权限,因此“r”应该就足够了。

To access without http just use the path to the file:要在没有 http 的情况下访问,只需使用文件路径:

$filename = '/path/to/file/text.txt';
$fp = fopen($filename, "r+");

Or if you want a relative path from the script itself I prefer:或者,如果您想要脚本本身的相对路径,我更喜欢:

$filename = dirname(__FILE__) . '/../some/relative/path/text.txt';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP:如何从FTP服务器将.txt文件读入变量? - PHP: How do I read a .txt file from FTP server into a variable? 我可以用PHP读取.TXT文件吗? - Can I read a .TXT file with PHP? 如何使用 php 将个人 append 文本/字符串转换为服务器上的 .txt 文件? - How can I make individual people append text/strings to a .txt file I have on my server using php? 我想从 txt 文件中读取数据,但 file_get_contents 拒绝读取 uaing Array 所以我如何读取它我的代码在这里 - I wanna read data from txt file but file_get_contents refuses to read uaing Array so how can i read it my code is here 我如何从txt文件到php var使用var - How can i use var from txt file to php var 如何通过php从服务器读取.doc文件并在android应用中显示.doc文件的文本? - How can i read .doc file from server via php and show the text of .doc file in android app? PHP可以读取和解释txt文件中的数据吗? - Can PHP read and interpret data from a txt file? 无法从php中的.txt文件正确读取 - Can't read correctly from a .txt file in php 如何使用Javascript(或PHP)读取.txt文件并将内容插入html网页? - How can I read a .txt file with Javascript(or PHP) and insert the contents into an html webpage? 如何使用php从.txt文件中读取\\ n为换行符 - how do I read a line break as \n from a .txt file using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM