upload foto

f3_hanung · Mar 14, 2012
mas mau nanya untuk tutorial ini : http://www.myphptutorials.com/tutorials/39/membuat-galeri-foto untuk upload fotonya agar dibatasi ukurannya misal, width=132px; height=122px dan type foto yang di perbolehkan hanya jpeg bgaiman ya mas??
Silahkan login untuk menjawab!
1
Loading...
Ellyx Christian · Mar 15, 2012 · 1 Suka · 0 Tidak Suka
ubah kode untuk upload menjadi:
<?php
//koneksi ke database
$conn = mysql_connect('localhost', 'userdb', 'password');
mysql_select_db('galeri');
//upload file
$allow = array('image/jpg','image/jpeg');
if(!empty($_FILES) && $_FILES['file']['size'] > 0 && $_FILES['file']['error'] == 0 && in_array($_FILES['file']['type'], $allow)){
	$data = getimagesize($_FILES['file']['tmp_name']);
	if($data[0] <= 132 && $data[1] <= 122){
		$fileName = $_FILES['file']['name'];
		$move = move_uploaded_file($_FILES['file']['tmp_name'], 'upload/'.$fileName);
		if($move){
			//simpan deskripsi dan nama file ke database
			$sql = "insert into foto (nama_file, deskripsi) values
					('$fileName', '".$_POST['deskripsi']."')";
			mysql_query($sql);
			header("Location: gallery.php");
			exit;
		}
	}
}
0
Loading...
f3_hanung · Mar 17, 2012 · 0 Suka · 0 Tidak Suka
mas Ellyx mau tanya lagi, saya coba masukkan script di ats yang saya sertkan kotak peringatan untuk batasi size dan type gambar,koq blum bisa ya...(bru belajar).mohon pencerahan, script saya seperti ini:
<?php
//koneksi ke database
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('galery');
$nama = $_POST;
$alamat= $_POST;

$tmpname = $_FILES;
$filetype = $_FILES;
$filesize = $_FILES;
$fileName = $_FILES;
$lokasifile = "../../upload/$fileName";
$allow = array('image/jpeg','image/jpg');
//upload file
if(empty($nama) || empty($alamat)){
echo"<script>alert(' >>> data tidak lengkap ! <<< ');document.location='from.html'</script>";
}
elseif($filesize < 0 || $_FILES != 0 || in_array($filetype,$allow)){
echo"<script>alert(' >>> gambar belum dipilih ! <<< ');document.location='form.html'</script>";
}
$data = getimagesize($filetype);
elseif($data[0] <=132 && $data[1] <=122){
echo"<script>alert(' >>> gambar max. 132 x 122 ! <<< ');document.location='form.html'</script>";
}
//simpan deskripsi dan nama image ke database
else{
move_uploaded_file($tmpname,$lokasifile);
$sql = "insert into foto(gambar,nama,alamat) values ('$fileName','$nama','$alamat')";
mysql_query($sql);
echo "<script>alert(' >>> data telah disimpan! <<< ');document.location='galery.php'</script>";
}
exit;
?>
0
Loading...
Ellyx Christian · Mar 17, 2012 · 0 Suka · 0 Tidak Suka
<?php
//koneksi ke database
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('galery');
$nama = $_POST["nama"];
$alamat= $_POST["alamat"];
 
$tmpname = $_FILES["file"];
$filetype = $_FILES["file"]["type"];
$filesize = $_FILES["file"]["size"];
$fileName = $_FILES["file"]["name"];
$lokasifile = "../../upload/$fileName";
$allow = array('image/jpeg','image/jpg');
$data = getimagesize($_FILES["file"]["tmp_name"]);
//upload file
if(empty($nama) || empty($alamat)){
echo"<script>alert(' >>> data tidak lengkap ! <<< ');document.location='from.html'</script>";
}
elseif($filesize < 0 || $_FILES["file"]["error"] != 0 || !in_array($filetype,$allow)){
echo"<script>alert(' >>> gambar belum dipilih ! <<< ');document.location='form.html'</script>";
}
elseif($data[0] >132 || $data[1] >122){
echo"<script>alert(' >>> gambar max. 132 x 122 ! <<< ');document.location='form.html'</script>";
}
//simpan deskripsi dan nama image ke database
else{
move_uploaded_file($tmpname,$lokasifile);
$sql = "insert into foto(gambar,nama,alamat) values ('$fileName','$nama','$alamat')";
mysql_query($sql);
echo "<script>alert(' >>> data telah disimpan! <<< ');document.location='galery.php'</script>";
}
exit;
?>