Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Upload File dan Pengelolaan File

Presentasi serupa


Presentasi berjudul: "Upload File dan Pengelolaan File"— Transcript presentasi:

1 Upload File dan Pengelolaan File

2 Upload File Upload merupakan proses penyalinan data yang ada dari komputer client ke komputer server. Faslilitas upload memungkinkan aplikasi web misalnya pendaftaran keanggotaan. Biasanya upload digunakan untuk mempermudah administrasi.

3 Handling File Uploads in PHP
PHP must run with the right settings. You may need to change the PHP configuration file php.ini. A temporary storage directory must exists with the correct permissions. Uploaded files are stored here temporarily; the files may be removed from here when your PHP script finishes processing the HTTP request. The final storage directory must exists with the correct permissions. You have to write codes to move the uploaded files from the temporary directory to another directory.

4 Configuring php.ini file_uploads max_input_time post_max_size
Enable/disable PHP support for file uploads max_input_time Indicates how long, in seconds, a PHP script is allowed to receive input post_max_size Size, in bytes, of the total allowed POST data upload_tmp_dir Indicates where uploaded files should be temporarily stored upload_max_filesize Size, in bytes, of the largest possible file upload allowed

5 File Upload Form upload.html: <html>
<head><title>PHP File Upload Form</title></head> <body> <form enctype="multipart/form-data“ action="upload.php” method="post"> <input type="hidden" name="MAX_FILE_SIZE” value=" "> File:<input type="file" name="userfile"><br> <input type="submit" value="Upload"> </form> </body> </html>

6 Character Encoding of Form-Data
<form enctype=“value”> The enctype attribute specifies how form-data should be encoded before sending it to the server. By default, form-data is encoded to "application/x-www-form-urlencoded”. This means that all characters are encoded before they are sent to the server Spaces are converted to "+" symbols Special characters are converted to ASCII HEX values).

7 Character Encoding <form enctype=“value”> Value Description
application/x-www-form-urlencoded All characters are encoded before sent (default setting) multipart/form-data No characters are encoded. This value is required when you are using forms that have a file upload control text/plain Spaces are converted to "+" symbols Special characters are not encoded

8 File Upload Form Label is automatically assigned

9 Receiving files $_FILES['userfile']['tmp_name']
name of the temporary copy of the file stored on the server. $_FILES['userfile']['name'] name of uploaded file. $_FILES['userfile']['size'] size of the uploaded file (in bytes). $_FILES['userfile']['type'] MIME type of the file such as image/gif. $_FILES['userfile']['error'] error that may have been generated as a result of the upload.

10 Upload error check $userfile_error = $_FILES['userfile']['error'];
if ($userfile_error > 0) { echo 'Problem: '; switch ($userfile_error){ case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; case 3: echo 'File only partially uploaded'; case 4: echo 'No file uploaded'; } exit; PHP.ini : upload_max_filesize = 2M HTML form : MAX_FILE_SIZE directive.

11 bool is_uploaded_file ( string $filename )
Returns TRUE if the file named by filename was uploaded via HTTP POST. This is useful to help ensure that a malicious user hasn't tried to trick the script into working on files upon which it should not be working --for instance, /etc/passwd.

12 bool move_uploaded_file ( string $filename , string $destination )
This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination.

13 Move the uploaded file $tempfile = $_FILES['userfile']['tmp_name'];
$userfile = $_FILES['userfile']['name']; // Destination file on server $destfile = '/var/www/html/a_______/temp' . $userfile; // Do we have an uploaded file? if (is_uploaded_file($tempfile)) { // Try and move uploaded file to local directory on server if (!move_uploaded_file($tempfile, $destfile)) { echo 'Problem: Could not move to destination directory'; exit; } else { echo 'Possible file upload attack. Filename: '. $userfile; echo 'File uploaded successfully<br /><br />'; Note: The target folder must exist for this example to work! See Upload.html, upload.php

14 Operasi File

15 Operasi File Secara umum format file dibedakan menjadi file teks (ASCII) dan file biner (binary). Contoh file teks adalah file-file dokumen HTML, termasuk didalamnya file script PHP, sedangkan file biner adalah file-file program atau file yang disimpan dalam format biner lainnya. Secara umum bekerja dengan file selalu mempunyai pola sebagai berikut : Buka file: Sintaks : $fhandle = fopen(”namafile”, ”mode”) Mode akses : r, r+, w, w+, a, a+, b (binary, agar tidak dibedakan sebagai teks atau biner). Proses File: Berisi perintah-perintah yang digunakan untuk melakukan pemrosesan file, bisa menulis ke file atau membaca data dari file. Menutup File: Sintaks : fclose ($fhandle)

16 Operasi File Membaca Data dari File
fgets() :$var = fgets ($fhandle, jumlahdata) Membaca data file secara baris per baris string. Setiap baris ditandai dengan adanya karakter ganti baris (newline). Jumlah data digunakan untuk membatasi jumlah byte yang harus dibaca. fread() : $var = fread($fhandle, jumlahdata) Melakukan pembacaan file dalam mode binary dalam mode yang aman (safe mode) dengan jumlah data yang dibaca per sekali baca adalah jumlahdata. fgetc() : $var = fgetc($fhandle) Membaca data file per karakter (satu karakter satu karakter). Menulis ke File fwrite() : fwrite($fhandle,string,length) menulis dalam mode binary safe (binary dan character bisa digunakan).

17 Operasi File

18 Operasi File

19


Download ppt "Upload File dan Pengelolaan File"

Presentasi serupa


Iklan oleh Google