Untuk demo disini dan download di sini.
Demo
Download
Tutorial Website Lengkap
Facebook login dengan javascript sdk
PHP menampilkan gambar dalam folder
PHP (gallery.php)
<table cellspacing="2" cellpadding="5">
<tr>
<?php
$folder = "./images/";
$handle = opendir($folder);
$i = 1;
$fileGambar = array('png', 'jpg', 'jpeg', 'gif');
while(false !== ($file = readdir($handle))){
$fileAndExt = explode('.', $file);
if(in_array(end($fileAndExt), $fileGambar)){
echo '<td style="border:1px solid #000000;" align="center">
<img src="'.$folder.''.$file.'" width="100" /><br />'.$file.'
</td>';
if(($i % 4) == 0){
echo '</tr><tr>';
}
$i++;
}
}
?>
</tr>
</table>
<table cellspacing="2" cellpadding="5">
<tr>
<?php
$folder = "./images/";
$handle = opendir($folder);
$i = 1;
$fileGambar = array('png', 'jpg', 'jpeg', 'gif');
while(false !== ($file = readdir($handle))){
$fileAndExt = explode('.', $file);
if(in_array(end($fileAndExt), $fileGambar)){
echo '<td style="border:1px solid #000000;" align="center">
<img src="'.$folder.''.$file.'" width="100" /><br />'.$file.'
</td>';
if(($i % 4) == 0){
echo '</tr><tr>';
}
$i++;
}
}
?>
</tr>
</table>
Jquery ajax post variable
HTML (ajax.html)
<html>
<head>
<title>Jquery ajax</title>
</head>
<script src="http://code.jquery.com/jquery-1.3.2.min.js"></script>
<script>
function dojqueryajax(coba){
$.post('ajax.php', {coba:coba}, function (data){
$('#hasil').text(data);
});
}
</script>
<body>
<a href="javascript:;" onclick="dojqueryajax('berhasil');">coba</a>
<div id="hasil"></div>
</body>
</html>
PHP (ajax.php)
<?php
echo $_POST['coba'];
?>
<html>
<head>
<title>Jquery ajax</title>
</head>
<script src="http://code.jquery.com/jquery-1.3.2.min.js"></script>
<script>
function dojqueryajax(coba){
$.post('ajax.php', {coba:coba}, function (data){
$('#hasil').text(data);
});
}
</script>
<body>
<a href="javascript:;" onclick="dojqueryajax('berhasil');">coba</a>
<div id="hasil"></div>
</body>
</html>
PHP (ajax.php)
<?php
echo $_POST['coba'];
?>
Upload gambar/image ke folder upload dengan PHP
1. Buat folder upload dan upload/upload, Membuat index.html di folder upload berfungsi
Berfungsi untuk menampilkan form upload
HTML (index.html)
<html>
<body>
<form action="uploader.php" method="post" enctype="multipart/form-data">
Filename:
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
2. Membuat uploader.php
Berfungsi untuk mengupload file gambar ke dalam folder upload/
PHP (uploader.php)
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: <textarea>http://www.persegres.com/upload/". $_FILES["file"]["name"]."</textarea>";
}
}
}
else
{
echo "Invalid file";
}
?>
3.Membuat galeri.php
Menampilkan semua gambar dalam folder upload/
PHP (galeri.php)
<?php
$folder = "upload/";
$handle = opendir($folder);
echo '<table cellspacing="2" cellpadding="5">';
echo '<tr>';
$i = 1;
$fileGambar = array('png', 'jpg', 'jpeg', 'gif');
while(false !== ($file = readdir($handle))){
$fileAndExt = explode('.', $file);
if(in_array(end($fileAndExt), $fileGambar)){
echo '<td style="border:1px solid #000000;" align="center">
<img src="'.$folder.''.$file.'" width="40" height="40" /><br />
'.$file.'
<a href="delete.php?file='.$file.'" onClick="location.href("oldwindowURL")">Delete</a> <br />
</td>';
if(($i % 4) == 0){
echo '</tr><tr>';
}
$i++;
}
}
echo '</tr>';
echo '</table>';
?>
Menghapus file gambar yang di pilih.
PHP (delete.php)
<?php
$file=$_GET['file'];
$fileName = 'upload/'.$file.'';
$delete=unlink($fileName);
if($delete){
header ("location:galeri.php");
}
else{
echo 'gagal';}
?>
Berfungsi untuk menampilkan form upload
HTML (index.html)
<html>
<body>
<form action="uploader.php" method="post" enctype="multipart/form-data">
Filename:
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
2. Membuat uploader.php
Berfungsi untuk mengupload file gambar ke dalam folder upload/
PHP (uploader.php)
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: <textarea>http://www.persegres.com/upload/". $_FILES["file"]["name"]."</textarea>";
}
}
}
else
{
echo "Invalid file";
}
?>
3.Membuat galeri.php
Menampilkan semua gambar dalam folder upload/
PHP (galeri.php)
<?php
$folder = "upload/";
$handle = opendir($folder);
echo '<table cellspacing="2" cellpadding="5">';
echo '<tr>';
$i = 1;
$fileGambar = array('png', 'jpg', 'jpeg', 'gif');
while(false !== ($file = readdir($handle))){
$fileAndExt = explode('.', $file);
if(in_array(end($fileAndExt), $fileGambar)){
echo '<td style="border:1px solid #000000;" align="center">
<img src="'.$folder.''.$file.'" width="40" height="40" /><br />
'.$file.'
<a href="delete.php?file='.$file.'" onClick="location.href("oldwindowURL")">Delete</a> <br />
</td>';
if(($i % 4) == 0){
echo '</tr><tr>';
}
$i++;
}
}
echo '</tr>';
echo '</table>';
?>
4. Membuat delete.php
Menghapus file gambar yang di pilih.
PHP (delete.php)
<?php
$file=$_GET['file'];
$fileName = 'upload/'.$file.'';
$delete=unlink($fileName);
if($delete){
header ("location:galeri.php");
}
else{
echo 'gagal';}
?>
Langganan:
Postingan (Atom)