I always have a doubt that I couldn't solve that is the problem of previewing an upload image properly.
I have wrote a php coding to upload and delete an image. In the page, user is able to view the image they have uploaded once they click the upload button. However, there is a few problem here. It is not working as expected.
The problem is as below:
When upload button is clicked, it shows the previous uploaded image instead of the new uploaded image. It will only show the updated image when the page is refresh. To do so, I include a <META http-equiv='refresh' content='0; URL=admin_news_events.php'> to refresh the page but it looks like the page and the image flip two times to show the updated image. The tag I used to display the image is <img src>. I wonder how to solve this problem. Can anyone please give me an idea. Thank you.
Here are the coding:
// when Upload Image button is clicked
if ( $upimg )
{
$uploadDir = 'C:/PortableWebAp/program/www/localhost/GreedySurvey/Administrator/Events/';
$fileName = $_FILES['imgfile']['name'];
$tmpName = $_FILES['imgfile']['tmp_name'];
$ext = substr(strrchr($fileName, "."), 1);
if(!(is_dir("Events")))
{
mkdir("Events");
}
if ( empty($imgfile) )
{
echo "ERROR: No file is given";
}
else if ( $ext != "jpg" )
{
echo "ERROR: Image files must be in jpg format";
}
else
{
$fileName = "Events.jpg";
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
echo "<META http-equiv='refresh' content='0; URL=admin_news_events.php'>";
// echo "Image has been uploaded successfully";
}
}
$img = "Events/Events.jpg";
if ( $delete1 )
{
if( file_exists($img) )
{
unlink($img);
echo "Image has been deleted";
}
}