用PHP读取文件-fopen/fread [英] Reading files with PHP - fopen/fread

查看:73
本文介绍了用PHP读取文件-fopen/fread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php

$top = "../top.txt";
$middle = "../middle.txt";
$bottom = "../bottom.txt";
$end = "/st.txt";
$data = "/dt.txt";

$handle1 = fopen($top, "r"); 
$contents1 = fread($handle1, filesize($top)); 
fclose($handle1); 

$handle2 = fopen($end, "r"); 
$contents2 = fread($handle2, filesize($end)); 
fclose($handle2);

$handle3 = fopen($middle, "r"); 
$contents3 = fread($handle3, filesize($middle)); 
fclose($handle3);

$handle4 = fopen($data, "r"); 
$contents4 = fread($handle4, filesize($data)); 
fclose($handle4);

$handle5 = fopen($bottom, "r"); 
$contents5 = fread($handle5, filesize($bottom)); 
fclose($handle5);

echo $contents1;
echo $contents2;
echo $contents3;
echo $contents4;
echo $contents5;

?>

每个错误我都会得到:

警告:fopen(../top.txt)[function.fopen]:无法打开流:没有此类文件或目录

警告:filesize()[function.filesize]:../top.txt

警告:fread():提供的参数不是有效的流资源

警告:fclose():提供的参数不是有效的流资源

所有文件和文件夹中的CHMOD均设置为777

CHMOD in all files and folders and is set to 777

所有文件都存在于服务器上

All the files exist on the server

PHP5已安装在服务器上

PHP5 is installed on the server

我在做什么错了?

推荐答案

$top = "../top.txt";

表示它从您当前的位置向上移动一个文件夹,并搜索文件top.txt. $middle = "../middle.txt";$bottom = "../bottom.txt";相同.对于这两个:

means that from your current position it moves one folder up, and searches for the file top.txt. It's same for $middle = "../middle.txt"; and $bottom = "../bottom.txt";. For these two:

$end = "/st.txt";
$data = "/dt.txt"

它是从根目录中搜索.确保确保文件位于引用文件的位置.我可能认为您在最后两个变量中需要这样做:

it's searching from the root directory. Do make sure you have the files located at the places you refer them to. I may think you wanted this in the last two variables:

$end = "./st.txt";
$data = "./dt.txt"

其中./代表当前目录.

因此,给您更详细的例子.假设您的PHP文件位于/var/www/httpdocs/project/phpFile.php,则文件的链接方式如下:

So, to give you more detailed example. Lets say you have your PHP file located at /var/www/httpdocs/project/phpFile.php then your files would be linked like:

$top = "/var/www/httpdocs/top.txt";
$middle = "/var/www/httpdocs/middle.txt";
$bottom = "/var/www/httpdocs/bottom.txt";

$end = "/st.txt";
$data = "/dt.txt"

现在,请注意最后两个变量的区别-它们保持不变,因为您已将其设置为从根目录(/)查找.好像您将使用与./的链接一样,那么路径将是:

So now notice the difference in the last two variables - they stay the same, because you've set it to be looking from root directory (/). Where as if you would use the linking with ./, then the paths would be:

$end = "/var/www/httpdocs/project/st.txt";
$data = "/var/www/httpdocs/project/dt.txt"

希望这可以解释它.是的,请研究答案中提到的其他人的功能.

Hope this explains it. And yes, do look into the functions other people mentioned in the answers.

这篇关于用PHP读取文件-fopen/fread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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