If you do any type of WordPress development for clients, then you’re likely familiar with having to work within many different environments.
Sure, the backend of each system may be very similar: That is, they are all running on some form of Linux with Apache or Nginx and MySQL. But, depending on the project that you’re working on, you may end up facing a variety of file permissions.
For example, let’s say that you’ve been hired to write a plugin or some custom functionality for someone and the work that you’re doing has to integrate with work that someone else has done. On top of that, it has to integrate with permissions on a file system that you can’t change.
Furthermore, a portion of the work you have to do must write a file to the disk. The problem? The code for saving a file isn’t working.
Touch a File
If you’re familiar with any type of Linux or Unix-based command-line work, then you’ve likely used the
touch command. PHP offers a function by the same name that does the same thing:
Attempts to set the access and modification times of the file named in the filename parameter to the value given in time. Note that the access time is always modified, regardless of the number of parameters. If the file does not exist, it will be created.
So why does this matter? Let’s say you have to write a file to the file system but:
- The file isn’t being created
- Nothing is being written to the error_log
- You’re seeing nothing being returned from the function or the console
- …or something else
Sometimes, using the simplest of functions can give you some insight on to why the functionality isn’t working. Sometimes, attempting to
touch a file before writing it to disk will help you write a better routine.
When working on client projects, I’ve found this to be helpful in a number of cases; however, it’s not a the “one solution to rule them all.”
Instead, it may just give enough information to ask if you can change directory permissions on the server. And if not, then perhaps you use a temporary location to write a file (like the
uploads directory) and then delete it when you’re done.
Either way, using a simple function like this can help you write better code and make more informed decisions when working on the solution you’ve been hired to create.
Touch a File (Before Creating It) was written by Tom. For more information about WordPress, development, and resources then visit Tom McFarlin’s blog.
Read more here:: Touch a File (Before Creating It)