Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

fopen - Opening/Reading a local file from PHP Web Application

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.

question from:https://stackoverflow.com/questions/65927388/opening-reading-a-local-file-from-php-web-application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...