Jump to content

Reading files


Hessuu

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.