Is this the correct way to check if a file exists?


beginner

I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far:

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

Is this the correct way to do this? Do I need to break the loop if a match is found? Wouldn't this be bad if there were a lot of pictures in that folder?

EDIT: Sorry, I should have mentioned: when uploading the file, the file was renamed with the user unique id, that's why if the user were to upload another image, I wanted to replace it with the existing image. (This image is for profile photo).

Facebook Facebook logo Sign up for Facebook to connect with Gufran Hasan

If you want to break the loop add break statementthe post unlink()function:

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

If you just want to check if the PHP file exists:

You can use this file_exist()function. This function returns TRUEif the file exists, otherwise 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 if the PHP file exists and is readable:

You can use this is_readable()function. The function returns TRUEif the file exists and is readable , otherwise 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 if a PHP file exists and is writable:

You can use this is_writable()function. The function returns TRUEif the file exists and is writable , otherwise 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);
}

Related


What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

Python way to check if file exists?

Juanjo Conti : What is the preferred way to check if a file exists and if it is created? collapse: To check if the path is an existing file: os.path.isfile(path) TrueReturns if path is an existing regular file . This is followed by a symlink so that both islin

Correct way to check if file exists in Linux

Brian I've been using it in my scripts but my colleagues disagree with me. My script accepts a file as a parameter and creates that file. Then I use the following to see if it actually gets created. ls -p | grep [filename] Then I check to see if the file I'm t

Efficient way to check if a file exists at startup

law I have provided this solution so far, but I am wondering if there is a more efficient way to do this. Fake code: public static void main(String args[]){ boolean FileNotFound = false; FileReader file = new FileReader("path"); try (BufferedReader bReader

What is the correct way to check if an array exists in JS?

Hussein When I check the length of the array, I get the following error. What is the correct way? main file if (drugPrice.mailPrice.rejectMessage.length !== 0 && Array.isArray(drugPrice.mailPrice.rejectMessage)) { //code goes here } mistake TypeError: Can

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

What is the correct way to check if a collection exists?

Tim Previously, I checked if the collection existed by querying the namespace. Roughly like this, check if "foo.bar" exists: return 1 === $client->selectCollection('foo','system.namespaces'); ->count(['name'=>'bar']); Since this only works

What is the correct way to check if an array exists in JS?

Hussein When I check the length of the array, I get the following error. What is the correct way? main file if (drugPrice.mailPrice.rejectMessage.length !== 0 && Array.isArray(drugPrice.mailPrice.rejectMessage)) { //code goes here } mistake TypeError: Can

What is the correct way to check if a global variable exists?

Ebrahim Byagowi: JSLint is not passing this as valid code: /* global someVar: false */ if (typeof someVar === "undefined") { var someVar = "hi!"; } What is the correct way? bigoldbrute: /*global window */ if (window.someVar === undefined) { window.so

Python way to check if file exists?

Juanjo Conti : What is the preferred way to check if a file exists and if it is created? collapse: To check if the path is an existing file: os.path.isfile(path) TrueReturns if path is an existing regular file . This is followed by a symlink so that both islin

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Correct way to check if DocumentDB object exists

Strenikovlev In Microsoft's example, I see two methods to check if a DocumentDb object exists, such as Database, DocumentCollection, Document, etc: The first is by creating a query: Database db = client.CreateDatabaseQuery().Where(x => x.Id == DatabaseId).AsEn

Is this the correct way to check if a file exists?

beginner I'm trying to check if a file (image) exists so I can delete it before uploading a new one. Here's what I've tried so far: foreach (glob("../imgs/" . $name . ".*") as $file) { if (file_exists($file)) { unlink($file); } } Is this the c

What is the correct way to check if a collection exists?

Tim Previously, I checked if the collection existed by querying the namespace. Roughly like this, check if "foo.bar" exists: return 1 === $client->selectCollection('foo','system.namespaces'); ->count(['name'=>'bar']); Since this only works

Python way to check if file exists?

Juanjo Conti : What is the preferred way to check if a file exists and if it is created? collapse: To check if the path is an existing file: os.path.isfile(path) TrueReturns if path is an existing regular file . This is followed by a symlink so that both islin