Home > Enterprise >  How can I make if values is empty how to set NULL in php
How can I make if values is empty how to set NULL in php

Time:02-16

I'm doing upload functions working fine but If I'm getting from uploaded folder, If folder don't have uploaded pdf in my upload folder in this time I want to make if values is empty how can I set NULL, I have did but issue is came.

Here what I'm getting error:

A PHP Error was encountered

Severity: Warning

Message: unlink(public/repair_pdfs/'repair_report022_02_15_12_56_37_00000065.pdf'): No such file or directory

Filename: controllers/test_api.php

Line Number: 3440

My Php Code is :

 for ($i = 0; $i < $names_size; $i  )
                {

                    $uploadPath = 'public/repair_pdfs/';

                    $filename_with_extension = $names[$i]->pdf;
                    
                  if (!empty($filename_with_extension)) {
                        
                        unlink($uploadPath . $filename_with_extension);
                    
                    } else {
                        
                        $filename_with_extension = "NULL";
                    }
                    

                   // unlink($uploadPath . $filename_with_extension);
                }

CodePudding user response:

Check your error message - path contains quotes. For some reason your $names[$i]->pdf contains quotes inside.

So you should fix what is adding additional quotes in $names[$i]->pdf or trim it to exclude quotes. Also before unlinking check if file exists

for ($i = 0; $i < $names_size; $i  ) {
    $uploadPath = 'public/repair_pdfs/';

    $filename_with_extension = trim($names[$i]->pdf, "'");

    if (!empty($filename_with_extension) && file_exists($uploadPath . $filename_with_extension)) {
        unlink($uploadPath . $filename_with_extension);
    } else {
        $filename_with_extension = null;
    }
    // unlink($uploadPath . $filename_with_extension);
}

  • Related