Hello,
I am trying to create a form which could upload image file and copy it to a certain folder and also updating the mysql database which store it's image name and title.
input_image.php
<div id="menu">
<center>
<h2>Image Manager</h2>
</center>
<p> </p>
<p>metode image managementnya masih konstruksi</p><center>
<p>
<?php
include('../includes/koneksi.php');
$id = isset($_POST['id']) ? $_POST['id'] : '';
$confirmation = isset($_POST['confirmation']) ? $_POST['confirmation'] : '';
$judul = isset($_POST['judul']) ? $_POST['judul'] : '';
$image = isset($_POST['image']) ? $_POST['image'] : '';
//Load berita
if (!empty($_REQUEST['id'])){
$result = mysql_query("SELECT * FROM gallery WHERE id =".$_REQUEST['id']) or die(mysql_error());
$data = mysql_fetch_array($result);
$id = $data['id'];
$judul = $data['judul'];
$image = $image['image'];
}
//Simpan berita
if (isset($_REQUEST['ok'])){
if (empty($_REQUEST['id']))
$sqlstr = "INSERT INTO gallery(judul, image) VALUES('".$judul."','".$image."')";
else
$sqlstr = "UPDATE gallery SET judul='".$judul."', image='".$image."' WHERE id=".$_REQUEST['id'];
$result = mysql_query($sqlstr) or die(mysql_error());
// cek type file & simpan file pada folder
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["image"]["name"]));
if ((($_FILES["image"]["type"] == "image/gif")
|| ($_FILES["image"]["type"] == "image/jpeg")
|| ($_FILES["image"]["type"] == "image/png")
|| ($_FILES["image"]["type"] == "image/pjpeg"))
&& ($_FILES["image"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
if ($_FILES["image"]["error"] > 0)
{
echo "Return Code: " . $_FILES["image"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["image"]["name"] . "<br>";
echo "Type: " . $_FILES["image"]["type"] . "<br>";
echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("c:\xampp\xampp\htdocs\rustoleum/photocms/" .
$_FILES["image"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["image"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],
"c:\xampp\xampp\htdocs\rustoleum/photocms/" . $_FILES["image"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"c:\xampp\xampp\htdocs\rustoleum/photocms/" . $_FILES["image"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
//Jika mode edit, maka tidak akan dikirimkan konfirmasi kepada subscriber
//if (empty($_REQUEST['id'])) kirimEmail($idKategori, $judul, $news);
$confirmation = ($result) ? "Data telah tersimpan." : "Gagal menyimpan data.";
}
?>
<div align="center">
<div style="width:800px;text-align:left;">
<script type="text/javascript" src="../../Masterlink/cgoods/ckeditor/ckeditor.js"></script>
<link href="../../Masterlink/cgoods/ckeditor/content.css" rel="stylesheet" type="text/css"/>
<?php echo $confirmation;?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<table>
<tr>
<th>Judul</th>
<th>Image</th>
</tr>
<tr>
<td><input size="30px" type="text" name="judul" value="<?php echo $judul; ?>"/></td>
</tr>
<tr>
<td><input size="30px" type="file" name="image" value="<?php echo $image; ?>"/></td>
</tr>
<tr><?php // showing the picture ?>
<td><img src="photos/<?php echo $image; ?>"/></td>
</tr>
<tr>
<td><input type="submit" name="ok" value="Simpan"/></td>
</tr>
</table>
</form>
</div>
</div>
The problem with this code is that when I try uploading an image called scenary (44 KB - JPEG). An error message "Invalid image" appears. I wonder why? I thought the image is small enough and has the correct file extension.