<html>
<?php
error_reporting(E_ALL ^ E_NOTICE);
mysql_connect("localhost","dbuser","dbpwd") or die (mysql_error());
mysql_select_db("upload2") or die (mysql_error());
?>
<form action="edit.php" method="POST" enctype="multipart/form-data">
<input type="file" name="myfile" required accept="image/jpeg"><br/>
<input type="submit" name="submit" value="Upload">
</form>
</html>
<?php
if($_POST['submit'])
{
$name=$_FILES['myfile']['name'];
$type=$_FILES['myfile']['type'];
$size=$_FILES['myfile']['size'];
$temp=$_FILES['myfile']['tmp_name'];
/* WARNING
For the sake of this tutorial, you have to change the id below to the image's id that you want to edit.
Untuk keperluan tutorial ini, anda harus menentukan id gambar mana yang mau anda edit */
$id=13;
$query=mysql_query("SELECT * FROM store WHERE id='$id'");
$row=mysql_fetch_array($query);
$old_location=$row['location'];
$delete_file=unlink($old_location);
if($delete_file)
{
if(!$name)
{
die("Error!");
}
else
{
if($type=="image/jpeg" && $size<102400) //file size is in Bytes
{
$timestamp=strtotime("now");
$file_name="img".$timestamp."-".$name."";
$location="img/$file_name";
move_uploaded_file($temp,$location);
$query=mysql_query("UPDATE store SET name='$file_name', location='$location' WHERE id='$id'");
echo "OK!";
}
else
{
echo "The format of the file is not allowed!";
}
}
}
else
{
echo "Ooops...!";
}
}
?>