📂 PHP File Handling
Estimated reading: 3 minutes 359 views

PHP File Existence / Copy / Delete / Download – Manage Files Securely and Efficiently

Learn how to check for file existence, copy and delete files, and initiate secure downloads using PHP file system functions.


Introduction – Why File Management Is Crucial

Every dynamic PHP application must be able to safely manage files — whether that means checking if a file exists, copying or deleting it, or allowing users to download files from the server.

In this guide, you’ll learn:

  • How to check if a file exists or is accessible
  • How to copy, move, or delete files
  • How to generate download responses
  • Best practices for secure and reliable file operations

1. Checking If a File Exists

if (file_exists("report.txt")) {
    echo "The file exists!";
}

file_exists() checks whether a file or directory exists.
Helps avoid errors before reading or deleting a file.


2. Verifying File Readability or Writability

if (is_readable("report.txt")) {
    echo "This file is readable.";
}

if (is_writable("report.txt")) {
    echo "This file is writable.";
}

Use is_readable() and is_writable() to validate file permissions.
Important for files handled by users or external sources.


3. Copying Files with copy()

copy("original.txt", "backup.txt");

Copies a file from the original path to a new destination.
Overwrites the target file if it already exists.


4. Deleting Files with unlink()

if (file_exists("old.txt")) {
    unlink("old.txt");
}

unlink() removes a file from the filesystem.
Always check for file existence before deleting to prevent warnings.


5. Renaming or Moving Files

rename("temp.txt", "archive/final.txt");

Moves or renames a file using the rename() function.
Can also move files across directories if permissions allow.


6. Forcing File Downloads with Headers

$file = "example.pdf";

if (file_exists($file)) {
    header("Content-Description: File Transfer");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
    header("Content-Length: " . filesize($file));
    readfile($file);
    exit;
}

Sends headers that prompt the browser to download the file.
Ensure secure access controls before exposing files for download.


7. Security Tips for File Operations

  • Always validate file paths to prevent directory traversal (../)
  • Sanitize file names before accessing them
  • Use realpath() to get absolute, resolved paths
  • Restrict downloadable directories to known folders
  • Don’t expose system or config files for download

Best Practices

  • Check file existence with file_exists() before accessing
  • Use unlink() cautiously and only after permission checks
  • Sanitize user-generated filenames and paths
  • Use MIME headers when handling file downloads
  • Log file operations if handling critical documents or media

Summary – Recap & Next Steps

Managing files in PHP using functions like file_exists(), unlink(), copy(), and secure downloads is essential for safe and smooth file handling in real-world applications.

Key Takeaways:

  • Use file_exists() and is_readable() before accessing files
  • Use unlink() to delete and copy() to clone files
  • rename() works for both moving and renaming
  • File downloads require proper MIME-type headers and sanitization

Real-World Use Cases:
Media managers, CMS systems, backup tools, download portals, admin dashboards.


Frequently Asked Questions (FAQs)

What’s the difference between unlink() and unset()?
unlink() deletes files from the disk, while unset() removes variables from memory.

Will copy() overwrite the destination file?
Yes, by default it replaces the destination file without warning.

Is it safe to use file names from user input?
Not directly. Always sanitize and validate before using in file functions.

Can I move files to another folder using PHP?
Yes — use rename("file.txt", "folder/file.txt").

How to force download of non-PDF files?
Use Content-Type: application/octet-stream and Content-Disposition: attachment.


Share Now :
Share

🗃️ PHP File Existence / Copy / Delete / Download

Or Copy Link

CONTENTS
Scroll to Top