Is this the correct way of checking if a file exists?
Asked Answered
A

2

6

I am trying to check if a file (image) exists so I can delete it before uploading the new one. This is what i've tried so far:

foreach (glob("../imgs/" . $name . ".*") as $file) {
    if (file_exists($file)) {
        unlink($file);
    }
}

Is this the correct way of doing this? Do I need to break the loop if a match is found? Would this perform badly if there are a lot of pictures in that folder?

Edit: sorry, i should have mention this: On upload the files get renamed with the user unique id, that's why if a user wants to upload another image i want to replace it with the existing one. (This images are used for profile pictures).

Autochthonous answered 9/4, 2018 at 7:19 Comment(7)
Please break statement after unlink() function.Forsake
While you are right Magnus, it looks tho, that he wants to check for whatever file extension, which you cant do using is_file.Lundell
@ManuelMannhardt - Surly they should know the file extension already, somehow? How would they otherwise be able to use the file?Tyrothricin
Well, if you are using glob, you dont need to know the extension, as OP has already in his code, he is searching for files with the given name and any extension. Glob returns an array containing all matched files then.Lundell
@ManuelMannhardt True, but my point is rather that with this code, there will only be one file with that name, regardless of the extension (since it always delete all of them, before uploading one).Tyrothricin
This is the case, if he doesnt have existing data yet. There might be old files where there are more then one file with a given name. Be paranoid! Always go for all cases you could think of.Lundell
I think we are assuming/guessing a bit about what the OP wants this for and what the application looks like. We don't really know enough to be able to answer what the "best" approach is.Tyrothricin
F
10

If you want to break the loop to add a break statement after unlink() function as:

foreach (glob("../imgs/" . $name . ".*") as $file) {
    if (file_exists($file)) {
        unlink($file);
        break;
    }
}

If you want to check PHP file exists only:

You can use the file_exist() function. This function returns TRUE if the file exists, otherwise, it returns FALSE.

$filename = './file_name.txt';

if(file_exists($filename)){
 echo sprintf('file %s exists',$filename);
}else{
 echo sprintf('file %s does not exist',$filename);
}

If you want to check PHP file exist and readable:

You can use the is_readable() function. The function returns TRUE if the file exists and is readable, otherwise, it returns FALSE.

$filename = './file_name.txt';

if(is_readable($filename)){
 echo sprintf('file %s exists and readable',$filename);
}else{
 echo sprintf('file %s does not exist or is not readable',$filename);
}

If you want to check PHP file exists and writable:

You can use the is_writable() function. The function returns TRUE if the file exists and is writable, otherwise it returns FALSE.

$filename = './file_name.txt';

if(is_writable($filename)){
 echo sprintf('file %s exists and writable',$filename);
}else{
 echo sprintf('file %s does not exist or is not writable',$filename);
}
Forsake answered 9/4, 2018 at 7:24 Comment(4)
Keep in mind, file_exists also returns true, if there is a directory with this name. So i´d use is_file over file_exists :)Lundell
Thanks, This file_exists() function checks if file exists in the same folder with the script file.Forsake
Yes, but since you cant know what $filename contains beforehand, I´d go the safe route and checking if it actually is a file.Lundell
Yes, Exactly @ManuelMannhardtForsake
L
3

Yes, your approach looks good to me. Its most likely faster then grabbing all file names and checking the names using regex.

As Gufran already mentioned in his comment, put a break after the unlink, so the loop does not continue to search. Do this only, if you dont plan to find multiple files, which that name (i.e. test.gif, test.png), then you have to leave it out.

Lundell answered 9/4, 2018 at 7:24 Comment(2)
Excactly. This is now up to OP, if he wants to delete all matching files (most likely the case), he has to leave it. If he only wants to delete first one, he needs it.Lundell
Sorry, just saw your last sentence :-)Tyrothricin

© 2022 - 2024 — McMap. All rights reserved.