How to Check if File Upload Has a Name in Php
Read Time: 5 mins Languages:
Many times you will either demand to motion files around or shop some information within them in PHP. In either case, knowing beforehand whether a file exists or non can help us avert some unexpected behavior.
PHP comes with a diversity of functions to handle dissimilar types of queries related to files. In this tutorial, I'll give you a cursory overview of all these functions and then that you tin option one that works best in your situation.
The Importance of Checking if a File Exists
There are a lot of scenarios in which checking if a file exists before doing anything else could be important. Let's say your website allows users to upload paradigm files on your server that they tin can access afterwards. It's fair to presume that there is always a chance of a clashing file name if many users are using your service to upload multiple files frequently.
In such cases, it becomes important to check if there is already another file at the location where you want to save the recently uploaded file of a user. You will then accept the choice to take some steps like renaming the file to something else or letting the user know that their upload will overwrite an existing file.
Permit's consider another situation where you are supposed to be appending information to a file in PHP. If the file you created to write all your data is deleted for some reason, functions like file_put_contents() will simply create a new file with the specified name and store your data inside the newly created file. This might exist desirable in some situations, but that won't always exist the case. And then it makes sense to cheque if the file exists beforehand if you already expect information technology to be there earlier you brainstorm writing your data.
Checking if a File Exists in PHP
There are three different functions that you can use to check if a file exists in PHP.
The beginning function is file_exists(). This function accepts a single parameter that is the path where your file is located. Keep in mind that it will render truthful for both existing files and directories. This may or may not be sufficient for your needs.
You can consider using the is_file() function if you want to be sure that the path you lot specified points to a file and not a directory. Similarly, you can use the is_dir() role to check if the path you specified exists and if information technology points to a directory.
<?php $name = "squares.txt"; $directory = "squares.zip"; if(file_exists($name)) { repeat 'The file "'.$name.'" exists.'; } if(is_file($name)) { echo '"'.$name.'" is indeed a file.'; } if(is_dir($directory)) { echo '"'.$directory.'" turned out to be a directory.'; } ?> output
In the to a higher place example, I intentionally named one of the directories squares.zip to evidence that it is important to practice your ain checks instead of assuming that the provided filename is actually a filename or a directory.
It is important to remember that both is_file() and is_dir() will render false fifty-fifty for paths that actually exist when the parent directory does non accept the right permissions.
Bank check if File Exists and is Readable or Writable
Two more than functions named is_readable() and is_writable() can be used to go some extra data about a file, besides checking if information technology exists.
As the name suggests, the is_readable() function volition check two things: first, that the file or directory actually exists, and second, that the file is readable. Similarly, the is_writable() function also checks ii things, that the file or directory exists and is writable.
<?php $proper noun = "squares.txt"; if(is_readable($name)) { repeat 'We can read "'.$name.'".'; } if(is_writable($name)) { echo 'We can also modify the contents of "'.$name.'".'; } ?> output
I would suggest you to be careful when interpreting the return value of these two functions. For instance, our first instinct when is_readable() returns faux is to remember that the file we queried is not readable. However, the role likewise returns simulated if the file does not be. It is important to ever keep this aspect of these functions in mind.
Beware of Cached Results
The return value that yous get back from a call to all these five functions—namely, file_exists(), is_file(), is_dir(), is_readable(), and is_writeable()—is cached. This means that repeated calls to a function, allow's say is_file(), could show you dried results.
PHP caches the results of these functions in social club to meliorate functioning. This makes certain that multiple calls for querying the aforementioned file piece of work faster. However, their render values will stay the same even if the file changes during the course of script execution.
The results are but cached for files that already be. This means that calling a part is_file() will keep returning false for non-existing files but volition start returning true as soon as the file is created. On the other paw, the office will keep returning true for a file that existed during the first phone call, even after the file has been deleted.
<?php $name = "squares.txt"; if(is_file($proper name)) { repeat '"'.$name.'" is indeed a file.'; } // Manually delete the file while script waits. sleep(5); if(is_file($name)) { echo '"'.$name.'" is indeed a file.'; } clearstatcache(); if(is_file($proper name)) { repeat '"'.$proper name.'" is indeed a file.'; } else { repeat 'The file probably no longer exists.'; } ?> If yous run the above code snippet for a file that really exists and then delete information technology manually while the script waits, calling is_file() will yet return true. However, you can get the right results by merely calling clearstatcache() before querying for the existence of the file again.
output
Some other thing to remember is that a phone call to unlink() automatically clears the cache, so you go fresh results for calls to functions similar is_file() subsequently.
Final Thoughts
We began this tutorial by learning the importance of checking the existence of files in PHP. After that, we learned about different functions that you tin can use to check if a file exists in PHP. We likewise learned virtually the advantages and disadvantages that some of these functions might have.
As I mentioned towards the cease, PHP will enshroud the results of some of these function calls to improve performance. Therefore, make sure that you clear the cache before doing something important with those files.
Did you find this post useful?
moldenhauerheye1936.blogspot.com
Source: https://code.tutsplus.com/tutorials/how-to-check-if-a-file-exists-in-php--cms-37074
Post a Comment for "How to Check if File Upload Has a Name in Php"