CodeIgniter Forums

Full Version: Help Needed : Upload File Exists Validation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]aslamdoctor[/eluser]
I am trying to develop a code to validate that the file which is about to upload is already exists or not,

for that I have to do something as following

Code:
if(!file_exists($config['upload_path'].$filename))
{
      $this->upload->do_upload();
}
else
{
      echo "File exists..."
}

now the problem here is, how do I get the $filename, if I use $_FILES , it will give the actual file name,
here I need the filename that CI generates during uploading by reducing special characters.

El Forum

[eluser]David Cassidy[/eluser]
The Upload library automatically checks whether or not the file exists. If so, it appends a sequential number to the end of the filename. You can change this behaviour if you like by extending the Upload class, or editing lines 358-377 in /system/libraries/Upload.php.

If you are just wanting a quick way to format the filename the way CodeIgniter does, the clean_file_name() method is a public function which means you can do:

Code:
if(!file_exists($config['upload_path'].$this->upload->clean_file_name($filename)))
{
      $this->upload->do_upload();
}
else
{
      echo "File exists..."
}

...to get the desired results.

El Forum

[eluser]aslamdoctor[/eluser]
Sorry, "clean_file_name()" is not for that purpose.

actually it worked with this


Code:
preg_replace("/\s+/", "_", $_FILES['userfile']['name'])