---
title: PHP File Handling
description: "> File handling is an important part of any web application. You often need to open and process a file for different tasks. > > > > Файл боловсруулах нь аливаа вэб програмын чухал хэсэг юм. Та янз бүрийн даалгаварт…"
url: "https://apprentice.mn/docs/php-file-handling/"
updated: "2021-07-19"
category: PHP
---

# PHP File Handling

> File handling is an important part of any web application. You often need to open and process a file for different tasks.
>
>
>
> Файл боловсруулах нь аливаа вэб програмын чухал хэсэг юм. Та янз бүрийн даалгаварт зориулж файл нээх, боловсруулах шаардлагатай байдаг.

---

## PHP Manipulating Files

PHP has several functions for creating, reading, uploading, and editing files.

PHP нь файл үүсгэх, унших, байршуулах, засах хэд хэдэн функцтэй байдаг.

**Be careful when manipulating files!**

**Файлуудтай харьцахдаа болгоомжтой байгаарай!**

When you are manipulating files you must be very careful.

Файлуудыг удирдахдаа та маш болгоомжтой байх хэрэгтэй.

You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.

Хэрэв та буруу зүйл хийвэл их хэмжээний хохирол учруулж болзошгүй юм. Нийтлэг алдаанууд нь: буруу файлыг засах, хатуу дискийг хог хаягдлын өгөгдлөөр дүүргэх, санамсаргүйгээр файлын агуулгыг устгах.

---

## PHP readfile() Function

The `readfile()` function reads a file and writes it to the output buffer.

`readfile()` функц нь файлыг уншаад гаралтын буферт бичдэг.

Assume we have a text file called “webdictionary.txt”, stored on the server, that looks like this:

Бидэнд сервер дээр хадгалагдсан “webdictionary.txt” нэртэй текст файл байна гэж үзье.

  AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The PHP code to read the file and write it to the output buffer is as follows (the `readfile()` function returns the number of bytes read on success):

Файлыг уншаад гаралтын буферт бичих PHP код дараах байдалтай байна ( `readfile()`  функц амжилттай уншсан байтын тоог буцаана):

### Example

### Жишээ

      `<?php
echo readfile("webdictionary.txt");
?>`

The `readfile()` function is useful if all you want to do is open up a file and read its contents.

Хэрэв та зөвхөн файл нээж, агуулгыг нь уншихад л хангалттай бол `readfile()`  функц нь ашиг тустай болно.

The next chapters will teach you more about file handling.

Дараагийн бүлгүүдээс файлтай харьцах талаар илүү их зүйлийг сурч мэдэх болно.

        [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-file-handling%2F&linkname=PHP%20File%20Handling](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-file-handling%2F&linkname=PHP%20File%20Handling)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-file-handling%2F&linkname=PHP%20File%20Handling](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-file-handling%2F&linkname=PHP%20File%20Handling)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-file-handling%2F&linkname=PHP%20File%20Handling](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fphp-file-handling%2F&linkname=PHP%20File%20Handling)[https://www.addtoany.com/share](https://www.addtoany.com/share)
