What are some common pitfalls to avoid when working with file handling functions in PHP?

One common pitfall to avoid when working with file handling functions in PHP is not properly checking for errors or handling exceptions. It's important to always check the return values of file handling functions and handle any potential errors that may occur during file operations.

// Example of properly checking for errors when opening a file
$file = fopen("example.txt", "r");
if ($file === false) {
    die("Unable to open file");
}
```

Another common pitfall is not closing files after performing operations on them. Failing to close files can lead to memory leaks and potential file corruption.

```php
// Example of properly closing a file after reading from it
$file = fopen("example.txt", "r");
if ($file) {
    $content = fread($file, filesize("example.txt"));
    fclose($file);
}
```

Additionally, it's important to sanitize user input when working with file paths to prevent security vulnerabilities such as directory traversal attacks.

```php
// Example of sanitizing user input for file paths
$filename = "uploads/" . basename($_FILES["file"]["name"]);
if (move_uploaded_file($_FILES["file"]["tmp_name"], $filename)) {
    echo "File uploaded successfully";
} else {
    echo "Error uploading file";
}