Rabu, 08 Februari 2012
0 komentar

Upload image dengan PHP



Sekarang kita akan membahas bagaimana mengupload image dengan PHP,langkah pertama yang dilakukan adalah buat halaman index.php yang isinya seperti berikut:
<html>
<head>
<title>Upload Gambar</title>
</head>
<body>
<?php
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext; }
$errors=0;
if(isset($_POST['Submit'])) {
$image=$_FILES['image']['name'];
if ($image) {
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "png")){
echo '<h1>extensi file harus PNG !!!</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024){
echo 'Ukuran melebihi limit';
$errors=1;}
$image_name=$image.'.'.$extension;
$newname=$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) { echo '<h1>Copy Berhasil!</h1>'; $errors=1;}}}}
//if(isset($_POST['Submit']) && !$errors) { }
?>
<center>
<form name="newad" method="post" enctype="multipart/form-data" action="">
<table>
<tr><td align="center">Upload Image</td></tr>
<tr><td><input type="file"></td></tr>
<tr><td><input name="Submit" value="Upload image"></td></tr>
<tr><td align="center">
<?php
$gmb=$image.".png";
$gbm="";
if (file_exists($gmb)) {
$gbm=$gmb;
} echo "<img src=$gbm width=80 height=80>";
?>
</td></tr>
</table>
</form>
</center>
</body>
</html>





 
Toggle Footer
Top