yttcom.net · Tech · PHP Education
← Back to Guides
Guide 03

Working With Files

PHP can read, write, create, and delete files on the server. This is the core of what every yttcom.net endpoint does.

Why does PHP need to touch files?

When you save a gym session, that data has to go somewhere permanent. When Builder deploys an HTML page, the file has to actually land on the server's hard drive. PHP is what makes that happen — it is the only thing that can write to the server's file system from a web request.

Analogy: PHP is the postal worker. Your curl command is the letter. PHP receives it, reads the address, walks to the right mailbox (folder), and puts the content inside.

Reading a file

file_get_contents() reads an entire file and gives you its content as a string.

$content = file_get_contents("/home/yttcooyc/public_html/data.txt"); // Always check if it worked if ($content === false) { echo "Could not read the file"; } else { echo $content; // outputs whatever is in data.txt }

Writing a file

file_put_contents() writes a string to a file. If the file doesn't exist yet, PHP creates it. If it does exist, PHP overwrites it.

// Write (creates file if missing, overwrites if exists) file_put_contents("/home/yttcooyc/public_html/data.txt", "Hello world"); // Append — add to end without erasing what's already there file_put_contents("/home/yttcooyc/public_html/log.txt", "New line\n", FILE_APPEND); // Write JSON data $data = ["status" => "ok", "count" => 5]; file_put_contents("/path/to/data.json", json_encode($data));

Checking if a file exists

Before you try to read or delete something, always check that it is actually there.

if (file_exists("/path/to/file.txt")) { echo "It exists"; } // file_exists() works on both files AND folders // is_file() — only returns true for actual files // is_dir() — only returns true for folders

Deleting a file

unlink() deletes a file permanently. No trash can — it is gone.

$path = "/home/yttcooyc/public_html/old_file.txt"; if (file_exists($path)) { unlink($path); echo "Deleted"; } else { echo "File not found"; }

Creating a folder

mkdir() creates a new directory. The true parameter lets it create multiple nested folders in one go.

// Create one folder mkdir("/home/yttcooyc/public_html/myfolder/", 0755); // Create nested folders — the true is important mkdir("/home/yttcooyc/public_html/systems/tech/", 0755, true); // 0755 means: owner can do everything, // everyone else can read and open (but not write)

Paths — finding the right location

Every file on a server has an absolute path — its full address from the root of the hard drive. PHP needs the full path, not just the filename.

// Full absolute path — always works $path = "/home/yttcooyc/public_html/data.txt"; // __DIR__ gives you the folder where the current PHP file lives // Use it to build paths relative to your script $log = __DIR__ . "/logs/activity.txt"; // If the PHP file is in /home/yttcooyc/public_html/ // then $log = /home/yttcooyc/public_html/logs/activity.txt
How file_write_web.php uses all of this

When Builder sends a curl command to deploy a page, file_write_web.php does exactly this sequence:

1. Receives the file path and content from the POST request
2. Builds the full server path using SERVER_ROOT + the path you sent
3. Checks if the destination folder exists — if not, calls mkdir() to create it
4. Calls file_put_contents() to write the content
5. Returns JSON saying how many bytes were written

That is it. Five steps. All file operations.

⚠️ Important: On yttcom.net, PHP cannot read or write files outside of /home/yttcooyc/public_html/. This is the open_basedir restriction — a security rule the hosting company sets. Everything stays inside public_html.