0

I feel like this should be a pretty straightforward process.

I have the following code:

<?php
    $filename = "c:/TestFolder/config.txt";
    echo "Attempting to read: ".$filename."<br/>";
    $fh = fopen($filename, 'r') or die("file doesnt exist");
    $readtext = fread($fh, filesize($filename));
    fclose($fh);
    echo "The Text is: ".$readtext;
?>

I have checked that I do indeed have "config.txt" in a folder called "TestFolder" on my C:/ drive... but I keep getting an error that the file doesn't exist.

I checked my PHPInfo to ensure that "allow_url_fopen" is turned on.

I have also tried different file path variations, such as:

C:\\TestFolder\\config.txt

C:\/TestFolder\/config.txt

This doesn't seem to make a difference.

Any idea what might be preventing me from opening this file?

Edits: It should be noted that this file is uploaded to my web host, while I am attempting to access a file on my local machine. Not sure if this changes things at all.

John Hubler
  • 877
  • 1
  • 11
  • 27
  • Check the `open_basedir` option. – Barmar Jan 27 '21 at 21:05
  • Looks like my open_basedir doesn't have a value in it currently. Should it? If so - what should it be? – John Hubler Jan 27 '21 at 21:07
  • Also - in case you missed the edits - I have this file uploaded to my web host, while attempting to open a file on my local machine. Not sure if that makes a difference or not. – John Hubler Jan 27 '21 at 21:09
  • "local machine" is the machine where PHP is running. That's the web host. – Barmar Jan 27 '21 at 21:11
  • sure it makes difference. if you uploaded some file and processing it with `php` you need first to `move_uploaded_file(...)` to some location and then open it. – spirit Jan 27 '21 at 21:11
  • Webservers can't access files on client machines directly, that would be an incredible security violation. You have to use a form that uploads the file to the server. – Barmar Jan 27 '21 at 21:12

1 Answers1

1

This is not possible. "local files" are files on the server where PHP is running, not the client running the web browser. While they might be the same machine when you're testing locally, once you upload the script to a web host, PHP tries to read files on the web host, not your machine.

The only way for PHP to access a file on the client machine is for the application to upload it.

Barmar
  • 741,623
  • 53
  • 500
  • 612