PHP Unlink Function

The PHP unlink function is a Boolean function that deletes an existing file on a Web server. You provide the path to the file as a parameter to the function. You can delete files of any size using unlink and doing so frees up the space or resources on your Web server.

  1. Uses

    • You can use unlink in a variety of situations. For example, pages that create temporary files that do not need to remain on the server after the visitor leaves the site can use unlink to remove those files. However, if you use a function such as fopen to create these files, you must use fclose to close them before deleting them. Alternatively, if you create a file management system for your server, use the unlink function to delete files similarly to how you use the rmdir function to delete folders.

    Parameters

    • The unlink function requires only one parameter, but you can provide up to two. The first parameter is the filename, which contains a path to a file. Here you can provide a full or partial URL, provided a partial URL maps to where the file exists on the server. The option parameter is the context, which is a group of resources that set or modify how the file stream behaves when you delete a file.

    Syntax

    • To call the unlink function, type "unlink($filename);" in your source code, remembering to include the semicolon at the end of the line. You can use a PHP variable as the filename parameter or a string enclosed in quotation marks with the path to the file. Unlike some other functions, such as fopen, you do not have to save the result in a variable because you don't have anything to do with it when it returns.

    Return Values

    • The unlink function returns true when it successfully deletes a file provided by the filename parameter. It returns false if it cannot delete the file, for example because the file does not exist or it is locked open elsewhere. When the function returns false, the Web page will display an error message. To avoid this, program an error handler function or use the unlink function in an if statement to handle a failed deletion attempt.

Related Searches:

References

Comments

Related Ads

Featured