[英]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.