If you’re working on a project for WordPress that’s going to allow users to upload files – be it a video, an image, a CSV, or any other type of data – then you’re likely going to be faced with a situation where you’re going to need to determine if a file is too large for WordPress.
What’s considered “too large” can be related to any number of factors:
- The size of the file is larger than you want to accept (or the file system accepts)
- A PHP timeout occurs when uploading a file because of its size
- The file system doesn’t have enough space
- …and so on.
Whatever the case may be, there are two things that you’re going to need to be able to do:
- Determine if the file fits within constraint of the system (whatever the constraint is)
- Display an error message to the user before the upload occurs
It doesn’t exactly provide for a stellar experience when trying to upload something only to have it rejected by the server without a proper error message, does it?
The File is Too Large For WordPress
As mentioned, how you determine if a file is too large for a given system depends on many of the environmental variables.
To that end, I can’t provide an example that covers every single scenario that you may hit, but there is a general setup that you can follow that only requires a change of implementation based on your use case. So in the following code, I’ll provide a general setup for how I’ve gone about structuring my code in these situations.
1. Setup the Notice Hook
This is your typical object-oriented WordPress code:
It’s a class definition with the hook name defined in the constructor and the actual function defined in the class. In our case, we’re going to need to do a bit of work in the hooked function in order to determine if the file is too large.
2. Check The File Size on Upload
Note that the following function assumes that you already have proper handling setup to determine if the $_FILES
array is even populated. That is, if a user is trying to upload a file but doesn’t provide any input, then that’s another case to be handled.
In this case, the user has provided a file so we do the following:
Having a private
function evaluate the size of the file (and there are other ways of checking the size and type of the file, but that’s not the point of this post) makes the code much more readable.
That is, when using the code code above, it will allow us to write code that reads like “If the files are too large, then display an error message,” which I’ll show in the next step.
I’m not saying that this is the best way to setup checking (because someone can always argue this), but I do find that when it comes to maintaining code – that is, reading it and working with it months or years after it’s been written — is much easier.
3. Display the Error (Or Upload The File)
Finally, we update the hooked function that we defined above in order to leverage the private function, display an error message, or proceed with the upload:
Note that the code checks to see if the file is too large and, if so, renders an error message. As the comment suggests, I’m more of a fan of keeping the markup out of the class but this is just for example purposes.
At any rate, this is the usual setup that I use when needing to check if a file is too large to upload. Your mileage may – and probably will – vary, but having some type of general skeleton for handling the operation is always helpful.
Leave a Reply
You must be logged in to post a comment.