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.
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.
Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this:
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):
The readfile() function is useful if all you want to do is open up a file and read its contents.
The next chapters will teach you more about file handling.
PHP Exercises
Fungsi PHP GLOB: dapatkan dan ubah hasilnya (PHP function GLOB: get and modify the results) I'm trying to import many xml files that I do not know the name. I use this code: I have a lot of files like agency.xml, annunci_324.xml, annunci_321.xml, ecc... I only need the files that begin for annunci and end .xml. I also need to delete last value's comma and put it in the last foreach. how can i do it?
I think you can check if name contains annunci with strstr function (documentation here) Now you can build directly your array without caring about commaslarutan
larutan
larutan 1:
This way we have all_urls as array of all the files starting with annunci and you can loop in it to simple_load them all.
(by Marcello Storit、Fabio)
larutan
- PHP function GLOB: get and modify the results (CC BY‑SA 3.0/4.0)
#PHP
Beberapa waktu lalu, kita telah membahas bagaimana cara export file antar komputer dengan menggunakan FTP pada PHP. Kali ini kita akan membuatnya menjadi otomatis tanpa harus menuliskan nama file nya terlebih dulu. Jadi, kita hanya menuliskan path lokasi file saja dan membacanya secara otomatis file yang ada di dalamnya.
Ok, langsung saja kita coba cara membuatnya. Pertama, kita tentukan dahulu lokasi path yang akan kita gunakan. Kemudian apakah hanya file tertentu saja ataukah semua file. Berikut adalah codenya :
$path = '/var/www/file/'; $files = glob("*.jpg"); foreach($files as $filename){ echo $filename; }Sedikit kita bahas code di atas.
1. $path
adalah lokasi file berada sesuai dengan lokasi yang kamu inginkan.
2. $files adalah variabel untuk mendefinisikan fungsi globe. Fungsi ini berfungsi untuk membaca file tergantung pola yang ditentukan.
3. Tanda * untuk membuat suat pola. Jika ditulis, maka berarti pencarian semua karakter. Semisal *.jpg adalah pencarian semua file dengan extensi jpg. Apabila ditulis dengan code *.*, makapencarian semua
file dan semua extensi.
4. foreach berfungsi untuk membuat perulangan sesuai dengan jumlah file dan memecahnya menjadi $filename.
5. $filename adalah parameter untuk menampilkan nama file yang telah didapatkan.
Dengan menggunakan code di atas, kita bisa melakukan export file ke server lain dengan menggunakan FTP tanpa harus menuliskan satu persatu file yang akan dikirim. Namun jika kamu tetap mau export nama file tertentu, kamu tetap harus mendefinisakan nama file tersebut.
Nah, demikianlah bagaimana cara Membaca Nama File dengan Glob pada PHP. Semoga dapat bermanfaat.
❮ PHP Filesystem Reference
Example
Return an array of filenames or directories that matches the specified pattern:
<?php
print_r(glob("*.txt"));
?>
The output of the code above could be:
Array (
[0] => target.txt
[1] => source.txt
[2] => test.txt
[3] => test2.txt
)
Definition and Usage
The glob() function returns an array of filenames or directories matching a specified pattern.
Syntax
Parameter Values
pattern | Required. Specifies the pattern to search for |
flags | Optional. Specifies special settings. Possible values:
|
Technical Details
An array of files/directories that matches the pattern, FALSE on failure |
4.3+ |
PHP 5.1: GLOB_ERR value added to the flags parameter |
More Examples
Example
Return an array of filenames or directories that matches the specified pattern:
<?php
print_r(glob("*.*"));
?>
The output of the code above could be:
Array (
[0] => contacts.csv
[1] => default.php
[2] => target.txt
[3] => source.txt
[4] => tem1.tmp
[5] => test.htm
[6] => test.ini
[7] => test.php
[8] => test.txt
[9] => test2.txt
)
❮ PHP Filesystem Reference