Question

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///
}
Was it helpful?

Solution 2

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

OTHER TIPS

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top