Unggah File PHP
Dengan PHP, mudah untuk mengunggah file ke server.
Namun, dengan mudah ada bahaya, jadi selalu berhati-hatilah saat mengizinkan unggahan file!
Konfigurasi File "php.ini"
Pertama, pastikan PHP dikonfigurasi untuk mengizinkan unggahan file.
Di file "php.ini" Anda, cari file_uploads
direktif, dan atur ke On:
file_uploads = On
Buat Formulir HTML
Selanjutnya, buat formulir HTML yang memungkinkan pengguna untuk memilih file gambar yang ingin mereka unggah:
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Beberapa aturan yang harus diikuti untuk formulir HTML di atas:
- Pastikan formulir menggunakan metode = "posting"
- Formulir juga memerlukan atribut berikut: enctype="multipart/form-data". Ini menentukan tipe konten mana yang akan digunakan saat mengirimkan formulir
Tanpa persyaratan di atas, unggahan file tidak akan berfungsi.
Hal lain yang perlu diperhatikan:
- Atribut type="file" dari tag <input> menunjukkan bidang input sebagai kontrol pemilihan file, dengan tombol "Jelajahi" di sebelah kontrol input
Formulir di atas mengirimkan data ke file bernama "upload.php", yang akan kita buat selanjutnya.
Buat File Unggah Script PHP
File "upload.php" berisi kode untuk mengunggah file:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType =
strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
skrip PHP menjelaskan:
- $target_dir = "uploads/" - menentukan direktori tempat file akan ditempatkan
- $target_file menentukan jalur file yang akan diunggah
- $uploadOk=1 belum digunakan (akan digunakan nanti)
- $imageFileType menyimpan ekstensi file dari file (dalam huruf kecil)
- Selanjutnya, periksa apakah file gambar adalah gambar asli atau gambar palsu
Catatan: Anda perlu membuat direktori baru bernama "uploads" di direktori tempat file "upload.php" berada. File yang diunggah akan disimpan di sana.
Periksa apakah File Sudah Ada
Sekarang kita dapat menambahkan beberapa batasan.
Pertama, kami akan memeriksa apakah file sudah ada di folder "upload". Jika ya, pesan kesalahan ditampilkan, dan $uploadOk disetel ke 0:
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
Batasi Ukuran File
Bidang input file dalam formulir HTML kami di atas bernama "fileToUpload".
Sekarang, kami ingin memeriksa ukuran file. Jika file lebih besar dari 500KB, pesan kesalahan ditampilkan, dan $uploadOk disetel ke 0:
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
Batasi Jenis File
Kode di bawah ini hanya memungkinkan pengguna untuk mengunggah file JPG, JPEG, PNG, dan GIF. Semua jenis file lainnya memberikan pesan kesalahan sebelum menyetel $uploadOk ke 0:
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
Selesai Unggah File Script PHP
File "upload.php" lengkap sekarang terlihat seperti ini:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType =
strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])).
" has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Referensi Sistem File PHP Lengkap
Untuk referensi lengkap fungsi sistem file, buka Referensi Sistem File PHP lengkap kami .