0

Currently my code can upload an image if a valid image is selected. What I need is if no image is selected, a default/alternative image will be uploaded instead. here is my code-

if(isset($_POST['btnSave'])){
    $imgName1 = $_FILES['image1']['name'];
    $imgTmp1 = $_FILES['image1']['tmp_name'];
    $imgSize1 = $_FILES['image1']['size'];

    $imgExt1 = strtolower(pathinfo($imgName1, PATHINFO_EXTENSION));
    $allowExt1  = array('jpeg', 'jpg', 'png', 'gif',''); //'' will allow empty value, otherwise will get error message
    $userPic1 = $carreg."-".$imgName1;
    if(in_array($imgExt1, $allowExt1)){
        if($imgSize1 < 7000000){
            move_uploaded_file($imgTmp1 ,$upload_dir.$userPic1);
        }else{
            $errorMsg = 'Image too large';
        }
    } else {
        $errorMsg = 'Please select a valid image (Approved extensions are jepg, jpg, png and gif)';
    }
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
S.N.Alam
  • 1
  • 4
  • Good code indentation would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Feb 19 '21 at 12:41
  • Well first you will need to test if an image was actually posted with this form submit, something you currently just assume will be the case – RiggsFolly Feb 19 '21 at 12:43
  • @RiggsFolly. thank you for your advice. I will try to learn and follow the coding standard. For now I have found a solution in this forum which is slightly different, but will serve the purpose. It will not upload an alternative file, but will show alternative file, if actual image is not available. 'https://stackoverflow.com/questions/7995080/html-if-image-is-not-found' – S.N.Alam Feb 19 '21 at 13:18

0 Answers0