Hello, I am new here, and hope to learn and contribute a lot. Recently, I started learning PHP (about 2 weeks), and now I am coming to the fopen() function. Say that I wanted to create a new file for reading and writing, is this what I would do?

$newfile = fopen("http://www.mydomain.org/php/Practice/text.txt", "a+");

Thanks in advance for any help,
Philip

Dani AI

Generated

Common cause of the behaviour described by is using an HTTP URL instead of a filesystem path. fopen with an HTTP URL uses the HTTP stream wrapper to read a remote resource — it will not create a file on the webserver the script is running on. For creating/writing files on the server use a filesystem path (absolute or relative). As mentioned, modes differ: r+ opens read/write but fails if the file does not exist; a+ will create the file if missing and position the pointer at the end. pointed toward the manual for the full mode list (see the PHP docs linked below).

A simple, safer way to write is file_put_contents (it handles opening/closing and supports atomic append with a lock). Example:

$path = __DIR__ . '/Practice/newfile.txt';
$bytes = file_put_contents($path, "This is a new file.\n", FILE_APPEND | LOCK_EX);
if ($bytes === false) {
    error_log("Write failed: " . print_r(error_get_last(), true));
} else {
    echo "Wrote $bytes bytes to $path";
}

Troubleshooting tips: enable errors while testing (ini_set('display_errors',1); error_reporting(E_ALL);), print getcwd() or __DIR__ to confirm where relative paths resolve, and check ini_get('allow_url_fopen') — writing to an http:// URL will not create a local file and usually requires a different approach (POST to a server endpoint, FTP/SFTP, or WebDAV). If using fopen, always check the return value and fclose() when done; PHP will close handles at script end but explicitly closing flushes buffers and releases locks sooner.

File-permission note: PHP must have write permission for the directory where the file is created. Prefer fixing ownership (chown) or using 0644/0755 rather than 0777. For more details see the PHP manual: fopen and file_put_contents.

Recommended Answers

All 4 Replies

$handle = fopen("http://www.example.com/", "r+");
'r+' : Open for reading and writing; place the file pointer at the beginning of the file.

Just making sure though, is this what I would do if I didn't have a file named text.txt, and I wanted to create one? And is this what I would use for the url?

"http://www.mydomain.org/php/Practice/text.txt"

EDIT: Also, do I have to close the file with fclose after I open it?

Ok, sorry for the double post, but I think I got it right. Here is the code to open, write, and close a new file I have:

<?php

$newfile = fopen("http://mydomain.org/php/Practice/mydata.txt", "a+");

fwrite($newfile, "This is a new file.");

fclose($newfile);

echo "All done!";

?>

Is that right? And if it is, how do I know if it worked, because when I run it, I can't find that new file on my server

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.