Hi,
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html> I have the code. Where would i save welcome.txt file. Will I save it on desktop or server side same directory as what i am currently working on?
Thanks.
Hi,
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html> I have the code. Where would i save welcome.txt file. Will I save it on desktop or server side same directory as what i am currently working on?
Thanks.
asked whether to save welcome.txt on the desktop or on the server. PHP runs on the server, so the file must be in a location the webserver/PHP process can read. As and implied, putting the text file inside the project folder under the server document root (or next to the PHP script) is the simplest option; using an absolute server path removes ambiguity.
A robust, minimal pattern that avoids the filesize/fopen pitfalls is shown below. It builds a path relative to the script and checks readability before output.
$path = __DIR__ . '/welcome.txt';
if (is_readable($path)) {
echo file_get_contents($path);
} else {
error_log("Cannot read $path");
echo 'Data file missing or not readable.';
} Notes and cautions: keep welcome.txt as .txt if it is raw data — change to .php only if server-side code inside the file must run. The page must be requested via HTTP (for example http://localhost/...) so PHP executes; opening the PHP file directly from the desktop (file://) will not run server-side code. Ensure file permissions allow the webserver user to read the file (on Unix-like systems, commonly 644 for files and 755 for directories). For reference, see the PHP documentation for file_get_contents (https://www.php.net/manual/en/function.file-get-contents.php).
Jump to Post— Stefano Mtangoo 455Save on server WAMP/LAMP and give it php extension!
Save on server WAMP/LAMP and give it php extension!
welcome.txt should save in a directory where your above php code file reside.
$file=fopen("welcome.txt","r");
$theData = fread($file, filesize('welcome.txt'));
echo $theData;
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.