upload file ke database

ayhu · Aug 8, 2011
selamat pagi mas,saya mau nanya,,,
saya punya aplikasi upload file,dan saya memberikan batasan kapasitas file yg bisa di upload max 2 MB,yang mau saya tanya kan bagaimana script nya untuk memberikan peringatan apabila ada file yg akan di upload melebihi dari 2 MB,,,,
tq
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Aug 12, 2011 · 0 Suka · 0 Tidak Suka
Saat kuliah kamu tidak diajarkan algoritma?
kasi saya kode form html-nya.
0
Loading...
ayhu · Aug 13, 2011 · 0 Suka · 0 Tidak Suka
saya tidak kuliah saya baru lulus SMK di sekolah dulu tidak pernah di ajarkan php,cuman html saja itu pun cuma dasar nya aja,,,
saya baru belajar buat aplikasi pakai php yg kayak bgini mas
mksud nya kode form html yg mna mas...?
maksud mas yang ini...?
<?php
include 'cek.php';
$username = $_SESSION['username'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<tr>
      <td><?php include('frame.php'); ?></td>
    </tr>
<body bgcolor="black" TEXT="white">
<H1 align="center"><MARQUEE align="center" direction="left" height="70" scrollamount="3" width="100%"behavior="alternate"> ARSIP DIGITAL PT SINAR GALESONG PRATAMA </H1></MARQUEE></font>
<HR size=5 color="blue">
<TABLE>
<br>
<form method="post" enctype="multipart/form-data" action="upload.php">
<table>
<tr><td><input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<TR>
<br>
<TD> Keterangan &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &nbsp; &nbsp; &nbsp; </TD>

<TD><INPUT TYPE="Text" NAME="keterangan" id="keterangan" SIZE="42"style="border-style: outset"></TD>
</TR>
</table>

<table>
<TR>
<br>
<TD> Bagian&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &nbsp; &nbsp; &nbsp; </TD>

<TD><INPUT TYPE="Text" NAME="bagian" id="bagian" SIZE="20"style="border-style: outset"></TD>

</TR>

</table>
<br>
<table>
<TR>

<TD> Tanggal &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : &nbsp; &nbsp; &nbsp; </TD>
<TD><select name="tahun" onchange="showKab()"Tipe="text"name="tanggal" id="tahun" style="border-style: outset">
<OPTION> -Tahun- </OPTION>

<OPTION> 2011 </OPTION>
<OPTION> 2012 </OPTION>
<OPTION> 2013 </OPTION>
<OPTION> 2014 </OPTION>
<OPTION> 2015 </OPTION>
<OPTION> 2016 </OPTION>

<OPTION> 2017 </OPTION>
<OPTION> 2018 </OPTION>
<OPTION> 2019 </OPTION>
<OPTION> 2020 </OPTION>
<OPTION> 2021 </OPTION>
<OPTION> 2022 </OPTION>

<OPTION> 2023 </OPTION>
<OPTION> 2024 </OPTION>
<OPTION> 2025 </OPTION>
<OPTION> 2026 </OPTION>
<OPTION> 2027 </OPTION>
<OPTION> 2028 </OPTION>

<OPTION> 2029 </OPTION>
<OPTION> 2030 </OPTION>
<OPTION> 2031 </OPTION>
<OPTION> 2032 </OPTION>
<OPTION> 2033 </OPTION>
<OPTION> 2034 </OPTION>

<OPTION> 2035 </OPTION>
<OPTION> 2036 </OPTION>
<OPTION> 2037 </OPTION>
<OPTION> 2038 </OPTION>
<OPTION> 2039 </OPTION>
</td>

<TD>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="bulan" onchange="showKab()"Tipe="text"name="tanggal" id="bulan" style="border-style: outset">
<OPTION> -Bulan- </OPTION>
<OPTION> 01 </OPTION>

<OPTION> 02 </OPTION>
<OPTION> 03 </OPTION>
<OPTION> 04 </OPTION>
<OPTION> 05 </OPTION>
<OPTION> 06 </OPTION>
<OPTION> 07 </OPTION>

<OPTION> 08 </OPTION>
<OPTION> 09 </OPTION>
<OPTION> 10 </OPTION>
<OPTION> 11 </OPTION>
<OPTION> 12 </OPTION></td>
<TD>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="tanggal" onchange="showKab()"Tipe="text"name="tanggal" id="tanggal" style="border-style: outset">
<OPTION> -Tanggal- </OPTION>
<OPTION> 01 </OPTION>

<OPTION> 02 </OPTION>
<OPTION> 03 </OPTION>
<OPTION> 04 </OPTION>
<OPTION> 05 </OPTION>
<OPTION> 06 </OPTION>
<OPTION> 07 </OPTION>

<OPTION> 08 </OPTION>
<OPTION> 09 </OPTION>
<OPTION> 10 </OPTION>
<OPTION> 11 </OPTION>
<OPTION> 12 </OPTION>
<OPTION> 13 </OPTION>

<OPTION> 14 </OPTION>
<OPTION> 15 </OPTION>
<OPTION> 16 </OPTION>
<OPTION> 17 </OPTION>
<OPTION> 18 </OPTION>
<OPTION> 19 </OPTION>

<OPTION> 20 </OPTION>
<OPTION> 21 </OPTION>
<OPTION> 22 </OPTION>
<OPTION> 23 </OPTION>
<OPTION> 24 </OPTION>
<OPTION> 25 </OPTION>

<OPTION> 26 </OPTION>
<OPTION> 27 </OPTION>
<OPTION> 28 </OPTION>
<OPTION> 29 </OPTION>
<OPTION> 30 </OPTION>
<OPTION> 31 </OPTION></td>
</TR>
</table>
<br>
<br>
Upload File &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:&nbsp; &nbsp; &nbsp; <input name="userfile" type="file"> (Max: 2MB)
    </td>
    <p><td><input name="upload" type="submit" value="Upload">
	</td>
</tr>
</table>
</form>
<br>
<br>

      <td><?php include('footer.php'); ?></td>
    </tr>
</html>

dan ini script upload.php sekarang sya pakai script yg kmrin mas kasi ke saya
0
Loading...
Ellyx Christian · Aug 13, 2011 · 0 Suka · 0 Tidak Suka
Seharusnya kamu itu diajarkan algoritma dan struktur data dulu, baru belajar bahasa pemrograman.
Ada sedikit salah di form html kamu, ini yang sudah saya perbaiki:
<?php
include 'cek.php';
$username = $_SESSION['username'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<tr>
 <td><?php include('frame.php'); ?></td>
 </tr>
<body bgcolor="black" TEXT="white">
<H1 align="center"><MARQUEE align="center" direction="left" height="70" scrollamount="3" width="100%"behavior="alternate"> ARSIP DIGITAL PT SINAR GALESONG PRATAMA</MARQUEE></h1>
<HR size=5 color="blue">
<TABLE>
<br>
<form method="post" enctype="multipart/form-data" action="upload.php">
<table>
<tr><td><input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<TR>
<br>
<TD> Keterangan &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &nbsp; &nbsp; &nbsp; </TD>
 
<TD><INPUT TYPE="Text" NAME="keterangan" id="keterangan" SIZE="42"style="border-style: outset"></TD>
</TR>
</table>
 
<table>
<TR>
<br>
<TD> Bagian&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: &nbsp; &nbsp; &nbsp; </TD>
 
<TD><INPUT TYPE="Text" NAME="bagian" id="bagian" SIZE="20"style="border-style: outset"></TD>
 
</TR>
 
</table>
<br>
<table>
<TR>
 
<TD> Tanggal &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : &nbsp; &nbsp; &nbsp; </TD>
<TD><select name="tahun" onchange="showKab()" id="tahun" style="border-style: outset">
<OPTION> -Tahun- </OPTION>
 
<OPTION> 2011 </OPTION>
<OPTION> 2012 </OPTION>
<OPTION> 2013 </OPTION>
<OPTION> 2014 </OPTION>
<OPTION> 2015 </OPTION>
<OPTION> 2016 </OPTION>
 
<OPTION> 2017 </OPTION>
<OPTION> 2018 </OPTION>
<OPTION> 2019 </OPTION>
<OPTION> 2020 </OPTION>
<OPTION> 2021 </OPTION>
<OPTION> 2022 </OPTION>
 
<OPTION> 2023 </OPTION>
<OPTION> 2024 </OPTION>
<OPTION> 2025 </OPTION>
<OPTION> 2026 </OPTION>
<OPTION> 2027 </OPTION>
<OPTION> 2028 </OPTION>
 
<OPTION> 2029 </OPTION>
<OPTION> 2030 </OPTION>
<OPTION> 2031 </OPTION>
<OPTION> 2032 </OPTION>
<OPTION> 2033 </OPTION>
<OPTION> 2034 </OPTION>
 
<OPTION> 2035 </OPTION>
<OPTION> 2036 </OPTION>
<OPTION> 2037 </OPTION>
<OPTION> 2038 </OPTION>
<OPTION> 2039 </OPTION>
</td>
 
<TD>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="bulan" onchange="showKab()"Tipe="text"name="tanggal" id="bulan" style="border-style: outset">
<OPTION> -Bulan- </OPTION>
<OPTION> 01 </OPTION>
 
<OPTION> 02 </OPTION>
<OPTION> 03 </OPTION>
<OPTION> 04 </OPTION>
<OPTION> 05 </OPTION>
<OPTION> 06 </OPTION>
<OPTION> 07 </OPTION>
 
<OPTION> 08 </OPTION>
<OPTION> 09 </OPTION>
<OPTION> 10 </OPTION>
<OPTION> 11 </OPTION>
<OPTION> 12 </OPTION></td>
<TD>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select name="tanggal" onchange="showKab()"Tipe="text"name="tanggal" id="tanggal" style="border-style: outset">
<OPTION> -Tanggal- </OPTION>
<OPTION> 01 </OPTION>
 
<OPTION> 02 </OPTION>
<OPTION> 03 </OPTION>
<OPTION> 04 </OPTION>
<OPTION> 05 </OPTION>
<OPTION> 06 </OPTION>
<OPTION> 07 </OPTION>
 
<OPTION> 08 </OPTION>
<OPTION> 09 </OPTION>
<OPTION> 10 </OPTION>
<OPTION> 11 </OPTION>
<OPTION> 12 </OPTION>
<OPTION> 13 </OPTION>
 
<OPTION> 14 </OPTION>
<OPTION> 15 </OPTION>
<OPTION> 16 </OPTION>
<OPTION> 17 </OPTION>
<OPTION> 18 </OPTION>
<OPTION> 19 </OPTION>
 
<OPTION> 20 </OPTION>
<OPTION> 21 </OPTION>
<OPTION> 22 </OPTION>
<OPTION> 23 </OPTION>
<OPTION> 24 </OPTION>
<OPTION> 25 </OPTION>
 
<OPTION> 26 </OPTION>
<OPTION> 27 </OPTION>
<OPTION> 28 </OPTION>
<OPTION> 29 </OPTION>
<OPTION> 30 </OPTION>
<OPTION> 31 </OPTION></td>
</TR>
</table>
<br>
<br>
Upload File &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:&nbsp; &nbsp; &nbsp; <input name="userfile" type="file"> (Max: 2MB)
 </td>
 <p><td><input name="upload" type="submit" value="Upload">
	</td>
</tr>
</table>
</form>
<br>
<br>
 
 <td><?php include('footer.php'); ?></td>
 </tr>
</html> 
dan ini untuk file upload.php
<?php
if(!$_POST){
	echo('Tidak ada data yang harus disimpan!');
}else{
	include 'cek.php';
	 // koneksi ke mysql
	mysql_connect('localhost','root','admin');
	mysql_select_db('dbname');
	$keterangan = $_POST['keterangan'];
	$bagian = $_POST['bagian'];
	$tanggal = $_POST['tahun'].'-'.$_POST['bulan'].'-'.$_POST['tanggal'];
	// membaca nama file
	$fileName = $_FILES['userfile']['name']; 
	// membaca size file
	$fileSize = $_FILES['userfile']['size'];
	if(empty($keterangan) || empty($bagian) || empty($tanggal)){ 
		echo '<strong>Data tidak komplit</strong>'; 
	}elseif($fileSize < 0 || $_FILES['userfile']['error'] != 0){
		echo 'Gagal upload file';
	}elseif($fileSize / 1048576 > 2){ //1 megabytes = 1048576 bytes
		echo 'Ukuran file melebihi 2MB';
	}elseif(mysql_num_rows(mysql_query("select * from upload where name='$fileName'")) > 0){
		echo 'File sudah ada didatabase';
	}else{
		// membaca nama file temporary
		$tmpName = $_FILES['userfile']['tmp_name']; 
 
		// membaca tipe file
		$fileType = $_FILES['userfile']['type'];
 
		// membaca username yang disimpan dalam session
		// username ini sekaligus menyatakan informasi pemilik file
		$username = $_SESSION['username'];
 
		// langkah membaca isi file yang diupload
		$fp = fopen($tmpName, 'r');
		$content = fread($fp, filesize($tmpName));
		$content = addslashes($content);
		fclose($fp);
 
		// query SQL untuk menyimpan file ke database disertai username pemiliknya
 
		$query = "INSERT INTO upload (name, size, type, content, username, keterangan, bagian, tanggal)
		VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$username', '$keterangan', '$bagian', '$tanggal')";
 
		mysql_query($query);
 
		// konfirmasi
 
		echo "<p>File ".$fileName." telah terupload</p>";
	}
}
?> 
0
Loading...
ayhu · Aug 18, 2011 · 0 Suka · 0 Tidak Suka
tq mas udah berhasil
saya mau nanya lagi kenapa aplikasi yg saya buat itu kalau di buka lewat komputer saya form nya tampil dengan teratur,tapi kalau di buka di komputer yang lain pakai ip address saya kok form nya langsung teracak(tersusun),kesalahan nya itu dimana mas,,,,?
0
Loading...
Ellyx Christian · Aug 18, 2011 · 0 Suka · 0 Tidak Suka
tanya teman terdekat kamu.
0
Loading...
ayhu · Aug 19, 2011 · 0 Suka · 0 Tidak Suka
ok dech mas,,,,tapi mas ada sedikit masalah mas sma form saya,,,
kok tiba2 form yg menampilkan semua data yang ada dalam database,tidak menampilkan satupun data (kosong),,,
pdhal script nya sya rasa sudah cocok,krna kemarin tampilan nya ada kok,masih bisa menampilkan semua data tapi tiba2 tadi saya coba buka lngsung eror gitu
ini mas saya kirim script saya tolong di cek yach

<?php
include 'cek.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Menampilkan Data</title></head> 
<tr>
      <td><?php include('frame.php'); ?></td>
    </tr>
<body BGCOLOR="#90EE90" TEXT="purple"font size="10">
<?php
mysql_connect("localhost","root","admin");
mysql_select_db("dbname");
$username = $_SESSION['username'];
echo "<h2 align='center'> Daftar File Anda </h2>"; 
$warnaGenap = "#CCCCCC";   // warna abu-abu
$warnaGanjil = "#FFFFFF";  // warna putih
$warnaHeading = "#FF0000"; // warna merah untuk heading tabel

$query = "SELECT * FROM upload WHERE username = '$username'";
$hasil = mysql_query($query);

echo '<table border="1" cellpadding="3" cellspacing="4">'; 
echo "<tr bgcolor='".$warnaHeading."'>";
echo '<td align="center"><B>ID</B></td>';
echo "<td align='center'><B>Keterangan</B></td>";
echo "<td align='center'><B>Tanggal</B></td>";
echo "<td align='center'><B>Bagian</B></td>";
echo "<td align='center'><B>Data Arsip</B></td>";
echo '<td colspan="2" align="center"><B>Action</td></B>';
echo "</tr>";

$counter = 1;
$conn=mysql_connect("localhost", "root", "admin");
mysql_select_db("dbname");
$sql="select * from upload WHERE username = '$username'";
$hasil=mysql_query($sql);
while($data = mysql_fetch_array($hasil))
{

// cek apakah counternya ganjil atau genap

if ($counter % 2 == 0) $warna = $warnaGenap;
else $warna = $warnaGanjil;

echo "<tr bgcolor='".$warna."'>";
echo "<td align='center'><B>".$data['id']."</B></td>";
echo "<td>".$data['keterangan']."</td>";
echo "<td>".$data['tanggal']."</td>";
echo "<td>".$data['bagian']."</td>";
echo "<td>".$data['name']."</td>";
echo "<td>";
echo "<a href='edit_arsip.php?id=".$data['id']."'> Edit </a>";
echo "</tr>";

$counter++; // menambah counter
}
echo "</table>";

?>

<br>
<br>
<tr>
      <td><?php include('footer.php'); ?></td>
    </tr>
0
Loading...
Ellyx Christian · Aug 19, 2011 · 0 Suka · 0 Tidak Suka
nama database kamu apa?
0
Loading...
ayhu · Aug 20, 2011 · 0 Suka · 0 Tidak Suka
dbname
0
Loading...
ayhu · Aug 24, 2011 · 0 Suka · 0 Tidak Suka
met pagi,,,
gmna mas prtnyaan sya kmrin,,,??
mas bisa bntu ndag..??
0
Loading...
Ellyx Christian · Aug 24, 2011 · 0 Suka · 0 Tidak Suka
errornya seperti apa?