Current Location: Home> Latest Articles> How to Avoid Accidentally Creating Empty Files with the touch() Function? Practical Tips You Should Know

How to Avoid Accidentally Creating Empty Files with the touch() Function? Practical Tips You Should Know

gitbox 2025-08-16

In PHP development, the touch() function is often used to update a file’s access and modification times, or to create an empty file if the file does not exist. However, many developers may encounter the issue of accidentally creating empty files when using touch(). Specifically, when the file does not exist, touch() will automatically create a new empty file. In certain situations, this can cause problems, particularly when you do not want empty files to be created unintentionally. This article will explore how to avoid this problem and share some practical techniques.

1. Understanding the Behavior of touch()

First, it’s important to understand the basic behavior of the touch() function. The touch() function modifies a file’s access and modification times. Its prototype looks like this:

<span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-title function_ invoke__">touch</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$filename</span></span><span> [, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$time</span></span> = </span><span><span class="hljs-title function_ invoke__">time</span></span>() [, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$atime</span></span> = </span><span><span class="hljs-title function_ invoke__">time</span></span>() ]] )  
</span></span>
  • $filename: The file name to operate on.

  • $time: The modification time, defaults to the current time.

  • $atime: The access time, defaults to the current time.

If the specified file does not exist and you don’t have permission to create it, touch() returns false. However, if the file does not exist and you have permission to create it, an empty file will be created and true is returned.

2. Preventing Accidental Empty File Creation

To prevent touch() from creating empty files when the file does not exist, you can use the following methods:

2.1 Check if the File Exists

The simplest and most effective way is to check whether the target file exists before calling touch(). You can use file_exists() or is_file() to verify the file’s existence. If the file does not exist, you can skip the touch() call.

<span>if (<span class="hljs-title function_ invoke__">file_exists</span>(<span class="hljs-variable">$filename</span>)) {  
    <span class="hljs-title function_ invoke__">touch</span>(<span class="hljs-variable">$filename</span>);  
} else {  
    <span class="hljs-comment">// File does not exist, handle differently or log it</span>  
}  
</span>

This ensures touch() is only called if the file already exists, avoiding the creation of empty files.

2.2 Use is_file() to Ensure It’s a File

Sometimes the target path may point to a directory instead of a file, which can cause incorrect behavior. Using is_file() ensures that the target is a file, not a directory, further preventing unnecessary empty file creation.

<span>if (<span class="hljs-title function_ invoke__">is_file</span>(<span class="hljs-variable">$filename</span>)) {  
    <span class="hljs-title function_ invoke__">touch</span>(<span class="hljs-variable">$filename</span>);  
} else {  
    <span class="hljs-comment">// Handle case where target is not a regular file</span>  
}  
</span>

2.3 Set File Permissions and Directory Checks

If you want to allow file creation when it does not exist, consider creating the target directory in advance and setting proper permissions to prevent unintended empty file creation. If the directory does not exist, you can create it first, then use touch().

<span>$dir = <span class="hljs-title function_ invoke__">dirname</span>(<span class="hljs-variable">$filename</span>);  
if (!<span class="hljs-title function_ invoke__">is_dir</span>($dir)) {  
    <span class="hljs-title function_ invoke__">mkdir</span>($dir, 0777, true); <span class="hljs-comment">// Create directory with permissions</span>  
}  
<span class="hljs-title function_ invoke__">touch</span>($filename);  
</span>

3. Using the @ Error Suppression Operator

Although using the error suppression operator @ is not considered best practice, if you only care about whether execution succeeds and not the specific error details, you can prepend @ before touch() to suppress warnings.

<span>@<span class="hljs-title function_ invoke__">touch</span>($filename);  
</span>

This prevents warnings when the file does not exist. However, it does not solve the empty file issue itself, so it’s best used with other strategies.

4. Custom File Creation Logic

If you need more control over file creation, you can implement custom file-handling logic. For example, in some cases, you might prefer to create a file with default content instead of leaving it empty. You can first check whether the file exists, and if not, create it with some initial content.

<span>if (!<span class="hljs-title function_ invoke__">file_exists</span>($filename)) {  
    <span class="hljs-title function_ invoke__">file_put_contents</span>($filename, "Initial content");  
} else {  
    <span class="hljs-title function_ invoke__">touch</span>($filename);  
}  
</span>

This approach avoids completely empty files while still providing default content.

5. Combining file_put_contents() or fopen() for More Control

If you need more flexible file creation and modification, use file_put_contents() or fopen(). These methods allow you to write content while creating the file, avoiding empty files altogether.

<span>if (!<span class="hljs-title function_ invoke__">file_exists</span>($filename)) {  
    <span class="hljs-title function_ invoke__">file_put_contents</span>($filename, "Initial content");  
}  
</span>

Or use fopen() to create the file and write to it if it does not exist:

<span>$handle = <span class="hljs-title function_ invoke__">fopen</span>($filename, "w");  <span class="hljs-comment">// 'w' mode creates the file and clears existing content</span>  
if ($handle) {  
    <span class="hljs-title function_ invoke__">fwrite</span>($handle, "Initial content");  
    <span class="hljs-title function_ invoke__">fclose</span>($handle);  
}  
</span>

Conclusion

When using the touch() function, the key to avoiding accidental empty file creation is to check whether the file exists beforehand or adopt alternative methods to prevent empty files from being generated. By combining functions like file_exists(), is_file(), and mkdir(), you can effectively prevent unnecessary empty files. Each approach has its own use case, and choosing the right strategy based on your specific requirements can improve both the robustness and maintainability of your code.