Hessuu Posted May 3, 2010 Share Posted May 3, 2010 I'm a php noob trying to create a php file which can read files from my school's server. The file in which the script should be has this kind of path: <ServerDomain>\<MyUserName>\<ProjectName>\public_html\index.php And this is the path of the files I need to read: <ServerDomain>\<MyUserName>\<ProjectName>\public_html\files\subfolder1\text.txt If I place the text file in the same directory as index.php it works, but if I move it in the subfolder, it gives me this message when trying to open it in browser: Warning: fopen(subfolder1\text.txt): failed to open stream: No such file or directory in <ServerDomain>\<MyUserName>\<ProjectName>\public_html\index.php on line 3 This is the code that causes this failure(it's from php.net's php documentation): $myFile = "files\subfolder1\text.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; Thanks for your time. Link to comment https://forums.phpfreaks.com/topic/200538-reading-files/ Share on other sites More sharing options...
anups Posted May 3, 2010 Share Posted May 3, 2010 HERE you are using backward slash (\) that is considered as escape character user following code $myFile = "files\\subfolder1\\test.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; OR the best way to user directory separator define("DS",DIRECTORY_SEPARATOR); $myFile = "files".DS."subfolder1".DS."test.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; Link to comment https://forums.phpfreaks.com/topic/200538-reading-files/#findComment-1052300 Share on other sites More sharing options...
ignace Posted May 3, 2010 Share Posted May 3, 2010 HERE you are using backward slash (\) that is considered as escape character user following code $myFile = "files\\subfolder1\\test.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; OR the best way to user directory separator define("DS",DIRECTORY_SEPARATOR); $myFile = "files".DS."subfolder1".DS."test.txt"; $fh = fopen($myFile, 'r'); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; The best way would probably be to use single quotes and realpath, like: $filepath = realpath('files\subfolder1\test.txt'); realpath modifies the directory separator to match the OS directory separator. Link to comment https://forums.phpfreaks.com/topic/200538-reading-files/#findComment-1052421 Share on other sites More sharing options...
Hessuu Posted May 4, 2010 Author Share Posted May 4, 2010 Thanks for the answers, now I know that. But now it gives me this: Warning: fread(): supplied argument is not a valid stream resource in /home/<SchoolUnit>/<MyUserId>/public_html/<ProjectName>/index.php on line 3 And same for the fclose. Current code: $myFile = realpath('subfolder1\test.txt'); $fh = fopen($myFile, r); $theData = fread($fh, filesize($myFile)); fclose($fh); echo $theData; Link to comment https://forums.phpfreaks.com/topic/200538-reading-files/#findComment-1052934 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.