1

I have a function which checks if image files exist. It works for all images, except when a period is inside the filename. The filenames are user uploaded, and many already exist that are not sanitized. Here is an example:

$img = 'nice_name.jpg'; // detects
$img = 'bad_name.7.jpg'; // doesn't detect

if (is_file($path . $img)) {
    return $path . $prefix . $img;
}

I'm not sure how to escape this or make it work. I have doubled checked and the file does exist at that path. The function works for other image names in the same folder.

edit: This was marked a duplicate and linked to a question about uploading files. I am using is_file() to check if a file already exists. There is no uploading occurring, and the file already has the extra "." in its name on the server, so this is a different issue.

1 Answers1

0

You can use basename() to get the file name, and then do something with it, like rename it if it contains a period.

$testfile = "test.7.img";
$extension = ".img";
$filename = basename($testfile, $extension);
if(strpos($filename,".") > 0) {
    $newname = str_replace(".","",$filename) . $extension ;
    rename($testfile,$newname);
}
//... then continue on with your code
devlin carnate
  • 8,309
  • 7
  • 48
  • 82