Вы находитесь на странице: 1из 9

Praktikum PHP 04

Operasi CRUD File Image

A. TUJUAN
Setelah melaksankan praktikum ini diharapkan siswa dapat:
1. Membuat koneksi database MySQL menggunakan PDO
2. Membuat script PHP untuk operasi CRUD File Image dengan metoda penyimpanan File System

B. PENDAHULUAN

Which is Better ? Saving Files in Database or in File System


Pros of the File system:
1. Performance can be better than doing it in db. To justify this, If you store large files in db then it may
slow down the performance because a simple query to retrieve the list of files or filename will also load
the file data if you used Select * in your query. While Files system accessing a file is quite simple and
light weight.
2. Saving the files and downloading them in the file system is much simpler than database since a simple
Save as function will help you out. Downloading can be done by addressing an URL with the location of
the saved file.
3. Migrating the data is an easy process here. You can just copy and paste the folder to your desired
destination while ensuring that write permissions are provided to your destination.
4. Cost effective as It is Economical in most of the cases to expand your web server rather than paying for
certain Databases.
5. Easy to migrate it to Cloud storage like Amazon S3 or CDNs etc in the future.

Cons of the File system:


1. Loosely packed. No ACID (Atomicity, Consistency, Isolation, Durability) operations relational mapping
which mean there is no guarantee. Consider a scenario if your files are deleted from the location
manually or by some hacking dudes, you might not know whether the file exists or not. Painful right?
2. Low Security. Since your files can be saved in a folder where you should have provided write
permissions, it is prone to safety issues and invites troubles like hacking. So it is best to avoid saving in fs
if you cannot afford to compromise in terms of security.

When is it most preferred:


1. If your application is liable to handle Large files of size more than 5MB and the massive number say
thousands of file uploads.  
2. If your application will have a large number of users.

Best way to do:


Though File System comes with some cost and certain cons, A good Internal Folder Structure and choosing a
folder location which may be a little difficult to access by others.

Page 1 of 9sync
Pros of Database:
1. ACID consistency which includes a rollback of an update that is complicated when the files are stored
outside the database.
2. Files will be in sync with the database so cannot be orphaned from it which gives you an upper hand in
tracking transactions.
3. Backups automatically include file binaries.
4. More Secure than saving in a File System.

Cons of Database:
1. You may have to convert the files to BLOB in order to store it in db.
2. Database Backups will become more hefty and heavy.
3. Memory ineffective. To add more, often RDBMS’s are RAM driven. So all data has to go to RAM first.
Yeah, that’s right. Had you ever thought about what happens when an RDBMS has to find and sort
data? RDBMS tracks each data page even lowest amount of data read/written, and it has to track if it’s
in memory or if it’s on disk if it’s indexed or sorted physically etc.

When is it most preferred:


1. If your user’s file needs to be more tightly coupled, secured and confidential.   
2. If your application will not demand a large number of files from a large number of users.

Best way to do:


1. Be cautious with your Select query, Avoid Unwanted Select * queries which may frequently retrieve the
file data unnecessarily.
2. Caching the file data can pave a way to reduce memory and database usage.
3. If you are using SQL server 2008 or higher version, make use of FILESTREAM.

Filestream enables storing BLOB data in NTFS while at the same time it ensures transactional consistency
between the unstructured BLOB data with a structured data in database.

C. PRAKTIKUM
Pada praktikum PHP 04 ini, Anda akan membuat web aplikasi sederhana yang mengimplementasikan
operasi CRUD pada file image dengan metoda penyimpanan melalui File System. Berikut adalah layout web
yang akan dibuat:

Page 2 of 9sync
1. Create database dengan nama “db_fileupload”.
2. Create sebuah table dengan nama “gambar” dengan struktur seperti berikut:

3. Create sebuah sub directory dengan nama “simpan_image” pada directory htdocs.
4. Create 2 buah sub directory dengan nama “images” dan “upload” pada
../htdocs/simpan_image seperti pada gambar berikut:

5. Create sebuah file php dengan nama “connection.php” pada directory sub directory
“simpan_image”, yang berisi script untuk koneksi MySQL seperti berikut ini:

Page 3 of 9sync
6. Create sebuah file dengan nama “index.php”, dengan script sebagai berikut:

7. Create sebuah file dengan nama “add.php”, dengan script sebagai berikut:

Page 4 of 9sync
Page 5 of 9sync
8. Create sebuah file dengan nama edit.php, dengan script sebagai berikut:

Page 6 of 9sync
Page 7 of 9sync
sync

Page 8 of 9sync
9. Create sebuah file dengan nama delete.php, dengan script sebagai berikut:

D. LATIHAN SOAL
1. Jelaskan apa yang dimaksud dengan file binaries?
2. Jelaskan apa yang dimaksud dengan tipe data BLOB !
3. Jelaskan kegunaan dari fungsi extract() !, tuliskan contoh implementasinya !
4. Jelaskan kegunaan dari fungsi unlink() .
5. Jelaskan secara rinci apa yang dimaksud dengan $_FILES Array (HTTP File Upload variables) !.
6. Jelaskan kegunaan dari fungsi move_uploaded_file() .

E. KESIMPULAN

Page 9 of 9sync

Вам также может понравиться