Re: [PHP] Problem with file reading



Delta Storm wrote:
I'm a beginner and i'm still learning PHP and I got a problem:
$file = "http://localhost/test_folder/test1.txt";; //I have also tried "test_folder/test1.txt" and "text1.txt"
$fh = fopen($file, "r") or die("Could not open file!");
$data = fread($fh, filesize($file)) or die ("Could not read file!");
fclose($fh);
echo $data;

The file exist, I'm using apache server on my PC for practicing and the file is located in the servers root folder on the subfolder "test_folder".

Is there a reason why you're trying to access it through a URL? If not, please don't.

The file path needs to be relative to the current file. So if your PHP file is in the root, "test_folder/test1.txt" should work. If your PHP file is in a folder named code in the root, "../test_folder/text1.txt" should work.

Also, depending on your PHP version you're probably better off using file_get_contents instead of fopen/fread/fclose.

-Stut
.



Relevant Pages

  • Re: [PHP] Problem with file reading
    ... I'm using apache server on my PC for practicing and the file is located in the servers root folder on the subfolder "test_folder". ... If your PHP file is in a folder named code in the root, ...
    (php.general)
  • Re: [PHP] Problem with file reading
    ... I'm using apache server on my PC for practicing and the file is located in the servers root folder on the subfolder "test_folder". ...
    (php.general)
  • Re: Secure Unix permissions for an Apache website developer
    ... > directory) and its files to be owned by root, ... developer part of that group with write permissions to the htdocs directory. ... Just do not make them owned by the user that the Apache server is ...
    (comp.os.linux.setup)
  • Re: Secure Unix permissions for an Apache website developer
    ... > directory) and its files to be owned by root, ... developer part of that group with write permissions to the htdocs directory. ... Just do not make them owned by the user that the Apache server is ...
    (comp.os.linux)
  • Re: Secure Unix permissions for an Apache website developer
    ... > directory) and its files to be owned by root, ... developer part of that group with write permissions to the htdocs directory. ... Just do not make them owned by the user that the Apache server is ...
    (comp.os.linux.security)