Yaj
Yaj

Reputation: 13

Check an image if existing already in the folder before uploading - Codeginiter

Do you have any sample codes or functions to check if an image name is existing already in the folder before uploading? I've tried using file_exists() but it doesn't work, here is my sample code:

$path = FCPATH . "images2/";
$filename=$_FILE['userfile'];
$full_path = $path .$filename;
if(file_exists($filename))
{
///display error message///
}

Upvotes: 1

Views: 5398

Answers (2)

jfindley
jfindley

Reputation: 712

Here is the simplest way to check if a file exist:

if(is_file($filename){
    return true; //the file exist
}else{
    return false; //the file does not exist
}

Upvotes: 1

Jakub
Jakub

Reputation: 20475

I'm assuming you are not getting the correct result with file_exists() because you don't include the full path (even tho you define it).

Try using the following: file_exists($full_path)

Also consider using some CI helper functions for handling files like images, or uploads. They are there to make this 'easier'.

File helper:
http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html

Upvotes: 0

Related Questions