Code for reuploading an image
<p>I have written a php code for uploading an image to store into database id, name, type and size and echo the image and it works fine. I need the user to have the opportunity to reupload in (update) the datadase (with the same id) a new image and delete the old one if image file is chosen. The result of the following code is to delete and to post nothing to the database.</p>

<pre><code>&lt;?php
if ($_FILES['avatarfile']['error'] == UPLOAD_ERR_OK)
$fileType = $_FILES['avatarfile']['type'];
$f = @fopen($_FILES['avatarfile']['tmp_name'], 'r');
if ($f === NULL){$fsize = @filesize($_FILES['avatarfile']'tmp_name']);
$fileData = addslashes(fread($f, $fsize));
fclose($f);
}
else
$f = @fopen($_FILES['avatarfile']['tmp_name'], 'r');
if ($f === NULL)
$fsize = @filesize($_FILES['avatarfile']['tmp_name']);
$fileData = addslashes(fread($f, $fsize));
fclose($f);
if(isset($_POST['submit']) &amp;&amp; !$errors &amp;&amp; $fsize &lt; 51200 &amp;&amp;(($_FILES['avatarfile']['type'] == "image/gif")|| ($_FILES['avatarfile']['type'] == "image/jpeg")|| ($_FILES['avatarfile']['type'] == "image/pjpeg") || ($_FILES['avatarfile']['type'] == "image/bmp") || ($_FILES['avatarfile']['type'] == "image/png") || $fileType == NULL))
{id=$_POST['id'];
mysql_query("UPDATE store_image SET avatar_image='$fileData', file_type='$fileType', file_size='$fsize' WHERE id='$_POST[id]' ") or die (mysql_error());
header('Location:index_4.php');
mysql_query("set character set 'UTF8'", $con);
if(!mysql_query($query)){echo 'problem'.mysql_error();  exit();}
}else {echo "You tried to upload a file that is not an image. Please select an image file";}
?&gt;
</code></pre>
