Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:09:32+00:00 2026-06-05T02:09:32+00:00

Because of PHP’s unlink() not supporting exceptions natively, I’m making a wrapper function for

  • 0

Because of PHP’s unlink() not supporting exceptions natively, I’m making a wrapper function for it. It should throw a FileNotFoundException if, well, the given file could not be deleted because it doesn’t exist.

For this, I need to determine whether the error thrown by unlink() was caused by a missing file or something else.

This is my test version for a custom delete function:

public function deleteFile($path){
    set_error_handler(function($errLevel, $errString){
        debug($errLevel);
        debug($errString);
    });
    unlink($path);
    restore_error_handler();
}

For $errLevel and $errString I get 2 (E_WARNING) and unlink(/tmp/fooNonExisting): No such file or directory

A rather bold approach would be like this:

if( strpos($errString, 'No such file or directory') !== false ) {
    throw new FileNotFoundException();
};

Question 1: How much can I rely on the error string being the same across different PHP versions? Question 2: Is there a much better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:09:33+00:00Added an answer on June 5, 2026 at 2:09 am

    I would simplify the code:

    public function deleteFile($path){
    
        if (!file_exists($path) {
            throw new FileNotFoundException();
        }else{
            unlink($path);
        }
    
        if (file_exists($path) {
            throw new FileNotDeleted();
        }
    }
    

    Then you don’t have to catch the $errstr and do complicated error catching. And it will work down to PHP 4 when exceptions were introduced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a social gaming network, for learning but mostly because php is alot
Before PHP 5.3.3 the following class Same { public function same() { echo 'Not
I assume the answer is: it is not possible because php is server language
is C# DateTime.UtcNow and php date(c) are equivalent I doubt because when I soap
I'm using CodeIgniter (because it's awesome) and I have something like: <?php echo anchor(/,
This problem drives me nuts, because the max_execution_time in the php.ini and in the
I'm currently using mysql w/ PHP because that's what I learned and haven't ever
I have tried several regex patterns (designed for use with PHP because I couldn't
Say I want a php script the run the function itstime() every hour. Other
I've just upgraded to PHP 5.3 and started supporting an old website for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.