Search code examples
phpphpmyadminblogsunlink

Is that correct way that this code i using (remove file/unlink) in php


I want to ask you guys about my code in my php update form.
I use unlink($file_Path) to Remove my image file in my folder & my server and that work fine!

So I wonder when using "unlink($file_Path)" to removing an image, In my php page it show me an "Image File Icon " when there was no picture (The image file was removed)
But it show me like that (Image file Icon) Because, I though It still has a some value (my value = "../filename.jpg")

so I put "value=" "/" (None Value to Reset a Value)
example : <input type="hidden" name="newSubimage1" value=""/>

and that work fine when I use with "!empty($ro.."
example :

if (isset($row['subimage1']) && !empty($row['subimage1']))
 {
    echo "<img src='../images/images_products/".$row['subimage1']."'>";  
 } else {
    echo "No Image Here"; }

`

to not show an "Image File Icon"
Is that a correct way ? that I used to Removing file / Not display an Image File Icon or you has any easy way or the best way that you want to suggest me.
thanks


Solution

  • $image_file_ext = array('jpg', 'png', 'bmp');
    $file_ext = pathinfo($row['subimage1'],PATHINFO_EXTENSION);
    
    if (isset($row['subimage1']) && !empty($row['subimage1']) && in_array($file_ext, $image_file_ext))
    {
        echo "<img src='../images/images_products/".$row['subimage1']."'>";  
    } else {
        echo "No Image Here"; 
    }