Web

Published on May 2016 | Categories: Types, Presentations | Downloads: 77 | Comments: 0 | Views: 490
of 9
Download PDF   Embed   Report

How to Upload and Download with PHP language

Comments

Content

14.
Upload dan Download File

File

Function
Mekanisme Upload File
Form untuk Upload File
Handle Upload File
Download File

Fungsi Dasar File
fopen


Open file atau URL.
int fopen ( string filename, string mode );

fclose


Close untuk pointer open file.
bool close ( int resource );

file_exists


Cek apakah file atau directory ada di system.
bool file_exists ( string filename );

copy


Copy file.
bool copy ( string source, string dest );

rename


Mengganti nama file
bool rename ( string oldname, string newname );

Mekanisme File
Agar bisa upload file ?





Buat form HTML dengan method POST
Tambahkan attribut form tsb dengan atribute type encrypsi, yaitu
anctype = “multipart/form-data”
Masukkan tag INPUT dengan type = “FILE” sebagai input file yang
akan diupload

Menangani upload file ?






Variable upload dengan menggunakan $_FILES atau
$HTTP_POST_FILES
File upload memiliki informasi : nama file, type, size, temporary
name, dan code error
File upload akan otomatis di-delete oleh system dari temporary
directory pada saat akhir request HTTP, jika file tsb tidak dipindah
(move) ke directory lain atau nama file diganti.

Form untuk Upload File
Sample :
<form enctype="multipart/form-data" action=“uploadfile.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
File Upload = <input name=“fileuploadku" type="file" />
<input type="submit" value="Send File" />
</form>

Catatan :
•File dikirim dengan method POST dan type encrypsi MULTIPART/FORM-DATA
•Kita bisa menambahkan informasi maksimal size file yang boleh diupload dengan
menggunakan tag INPUT dengan nama MAX_FILE_SIZE
•File upload dengan menggunakan nama variable fileuploadku

Handle Upload File
Informasi temporary directory untuk handle file upload bisa di lihat/setting di configurasi PHP, yaitu
php.ini pada directive upload_temp_dir
Isi informasi file upload dengan $_FILES sbg (ikuti sample) :

$_FILES [ ‘ fileuploadku ‘ ] [ ‘ name ‘ ]
 nama asli file yg diupload dari mesin client.

$_FILES [ ‘ fileuploadku ‘ ] [ ‘ type ‘ ]
 Type MIME dari file yang berkorespondensi dengan browser, misal untuk file GIF, maka type
= "image/gif".

$_FILES [ ‘ fileuploadku ‘ ] [ ‘ size ‘ ]
 size file upload dalam satuan byte.

$_FILES [ ‘ fileuploadku ‘ ] [ ‘ tmp_name ‘ ]
 Nama file temporary saat file ditangani di server.

$_FILES [ ‘ fileuploadku ‘ ] [ ‘ error ‘ ]
 Code error saat upload file :
UPLOAD_ERR_OK = 0 ; no error , sukses.
UPLOAD_ERR_INI_SIZE = 1; size file upload tidak sesua aturan upload_max_filesize di php.ini.
UPLOAD_ERR_FORM_SIZE = 2 ; size file upload tdk sesuai aturan MAX_FILE_SIZE yg ada di form HTML.
UPLOAD_ERR_PARTIAL = 3 ; file uplaod terkirim secara partial
UPLOAD_ERR_NO_FILE = 4 ; tidak ada file yang upload.
UPLOAD_ERR_NO_TMP_DIR = 6 ; temporary folder tidak valid.
UPLOAD_ERR_CANT_WRITE = 7 ; error saat write file ke disk di server.

Handle Upload File (Cont.)
Sample :
<?
$uploaddir = '/kumpulanfileupload/';
$uploadfile = $uploaddir . basename ( $_FILES [ ‘ fileuploadku ‘ ] [ ‘ name ‘ ] );
if ( move_uploaded_file ( $_FILES [ ‘ fileuploadku ‘ ] [ ‘ tmp_name ‘ ] , $uploadfile ) ) {
echo “SUKSES UPLOAD";
} else {
echo “GAGAL UPLOAD";
}
?>
Catatan :
•Folder untuk menyimpan file upload ini pada folder “kumpulanfileupload”
•Fungsi basename untuk hanya mengambil nama file saja jika argument fungsi ini ternyata
lengkap dengan path file. Dalam kasus ini, berguna untuk hanya mengambil nama file saja.
•File upload yang ada di temporary directory di pindah (move_upload_file) ke folder
kumpulanfileupload agar tidak terhapus oleh system saat akhir request HTTP

Multiple Upload File : Form
Sample :
<form enctype="multipart/form-data" action=“uploadfile.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
File Upload 1 = <input name=“fileuploadku [ ]" type="file" />
File Upload 2 = <input name=“fileuploadku [ ]" type="file" />
File Upload 3 = <input name=“fileuploadku [ ]" type="file" />
<input type="submit" value=“Kirim File" />
</form>

Catatan :
•Gunakan nama variable untuk tag INPUT FILE dengan bentuk array, yaitu dengan
penambahan [ ] diakhir nama variable sehingga menjadi fileuploadku[]

Multiple Upload File : Handle
Sample :
<?
$uploaddir = '/kumpulanfileupload/';
foreach ($_FILES [ ‘ fileuploadku ‘ ] [ ‘ error ‘ ] as $key => $error ) {
if ( $error == UPLOAD_ERR_OK ) {
$tmp_name = $_FILES [ ‘ fileuploadku ‘ ] [ ‘ tmp_name ‘ ] [ $key ];
$uploadfile = $uploaddir . basename ( $_FILES [ ‘ fileuploadku ‘ ] [ ‘ name ‘ ] [ $key ] );
if ( move_uploaded_file ( $tmp_name, $uploadfile ) ) {
echo “SUKSES UPLOAD <br>”;
} else {
echo “GAGAL UPLOAD <br>”;
}
}
}
?>
Catatan :
•Cara handlenya juga menggunakan array, dimana pada sample diatas dianggap object
array dengan menggunakan statement foreach dan variable index $key

Download File
Bisa dengan menggunakan Link URL, yaitu tag <a>
Misalkan file ZIP atau TAR, maka oleh browser akan dilakukan downlaod file tsb.
Sample:

<a href=“/kumpulanfileupload/data.zip”>download data</a>

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close