Beranda > Newbie, Web > Form Upload (Mempermudah transaksi File dalam Pembelajaran berbasis Komputer)

Form Upload (Mempermudah transaksi File dalam Pembelajaran berbasis Komputer)


Salah  satu  hal  terpenting  ketika mempelajari  PHP  adalah mengenai  upload  file  dan berbagai macam mekanismenya, dalam tutorial dibawah kita akan belajar :

  1. Membuat form untuk upload file
  2. Membuat script upload sederhana
  3. Menambahkan fitur file_exists untuk mendekti upload file yang sama
  4. Menambahkan fitur filter untuk upload jenis file tertentu

script form_upload.html

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<meta name=”description” content=”PHP script upload file” />
<meta name=”keywords” content=”PHP tutorial, PHP” />
<meta name=”author” content=”kinta mahadji” />
<title>Upload script</title>
</head>

<body>
<form action=”upload.php” method=”post” enctype=”multipart/form-data” name=”form1″ id=”form1″>
<h3>Upload file :</h3>
<label>
<input type=”file” name=”fupload” />
</label>
<p>
<label>
<input type=”submit” name=”upload” value=”Upload” />
</label>
</p>
<p><a href=”files/”>Browse files </a></p>
</form>
<p>Created by : Bowo Priyanto, <a href=”http://komputerblog.com”>http://komputerblog.com</a&gt; </p>
</body>
</html>

Script Upload.php

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<meta name=”description” content=”PHP script upload file” />
<meta name=”keywords” content=”PHP tutorial, PHP” />
<meta name=”author” content=”kinta mahadji” />
<title>Upload script</title>
</head>

<body>
<p>
<?php

//Membaca nama file
$file_name = $_FILES[‘fupload’][‘name’];
//Membaca ukuran file
$size = $_FILES[‘fupload’][‘size’];
//Membaca jenis file
$file_type = $_FILES[‘fupload’][‘type’];

//Source tempat upload file sementara
$source = $_FILES[‘fupload’][‘tmp_name’];
//Tempat upload file disimpan
$direktori = “files/$file_name”;

//Mengecek apakah file yang di upload sudah ada atau belum
if( file_exists ($direktori)) {
echo “file <strong>$file_name</strong> sudah ada, upload dengan nama lain <br/> <a href=\”form_upload.html\”>kembali</a>”;
exit();
} elseif ($file_type != “image/gif” && $file_type != “image/jpg” && $file_type != “image/jpeg” && $file_type != “image/png”) {
echo $file_type.”<br/>”;
echo “file <strong>$file_name</strong> tidak di support, hanya untuk upload gambar (gif, jpg,jpef,png)”;
} else {
//Memindahkan upload file dari direktori sementara ke tempat permanen
//move_uploaded_file( $source, $target );// or die (“Couldn’t copy”);
move_uploaded_file($source,$direktori);

//Menampilkan keterangan file
echo “<strong>Direktori sementara :</strong> “.$source.”<br/>”;
echo “<strong>Folder :</strong> “.$direktori.”<br/>”;
echo “<strong>Filename :</strong>”.$file_name.”<br/>”;
echo “<strong>Size : </strong>”.$size.” bytes <br/>”;
echo “<strong>File type : </strong>”.$file_type.”<br/>”;
echo “<strong>Filename :</strong>”.$file_name.”<br/>”;
}

?>

</p>
<p><a href=”form_upload.html”>Upload lagi</a> | <a href=”files/”>Browse files </a></p>
<p>Created by : kinta mahadji, <a href=”http://komputerblog.com”>http://komputerblog.com</a&gt; </p>
</body>
</html>

Kemudian simpan dalam satu Folder, jangan lupa didalam folder dibuat sebuah file bernama upload

Alamat download File : uploadfile.zip
Alamat download Tutorial : php-upload-file.pdf

Terimakasih
-Bowo Priyanto dan http://ilmukomputer.org/2009/08/29/php-upload-file/

Kategori:Newbie, Web
  1. 12 Februari 2013 pukul 7:32 pm

    “Form Upload (Mempermudah transaksi File dalam Pembelajaran berbasis
    Komputer) Media Berbagi tanpa batas” in fact enables myself imagine a small bit more.
    I really cherished each and every single portion of it.

    Regards -Heriberto

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: