0

I got ascript which helps me to add some data into a csv file, based on the fact if a image is inside a folder or not (exits or not). Files are images, so I need to check if the file exists, and if it is a png, jpg, jpeg, gif.

So far it only check if it a JPG but I would like it to find the file exists if it's a PNG or JPEG or even GIF.

<?php
$columns = array("row1","row2","row3","row4","row5","row6","row7","row8","row9",
"row10","row11","row12","row13","row14","row15","row16","row17","row18"
);
$rootDir = "/path/to/images/folder/files";
$file = fopen("database.csv", "r") or die('fopen database failed');
$newFile = fopen("newdata.csv", "w") or die('fopen newdata.csv failed');
while (($data = fgetcsv($file, 999999, ";")) !== FALSE) {
    $row = array_combine($columns, $data);
    $filename = $row['row4'].".jpg"; // could be png or jpEg, or even gif
    if (file_exists("$rootDir/$filename")) {
        $row['image'] = .$filename; //also needs correct extension of image which exists.
        $row['small_image'] = .$filename;
        $row['thumbnail'] = .$filename; 
    }
    fputcsv($newFile, array_values($row), ";",'"' );
}
fclose($file);
fclose($newFile);
?>
mlclm
  • 725
  • 6
  • 16
  • 38
  • You can make an array with the extensions and put `$filename = $row['row4'].".jpg"; // could be png or jpEg, or even gif if (file_exists("$rootDir/$filename")) { $row['image'] = .$filename; //also needs correct extension of image which exists. $row['small_image'] = .$filename; $row['thumbnail'] = .$filename; }` in a for to check the extensions – Thomas Rollet Dec 18 '15 at 14:06
  • 1
    suggestion: check [exif_imagetype](http://php.net/manual/en/function.exif-imagetype.php), it can give you better image type details than checking for extension – bansi Dec 18 '15 at 14:09
  • So, `row4` contains the root of the filename with no extension? – Jesse Williams Dec 18 '15 at 14:27
  • @mlclm, Is your question still actual for you? I have some good solution(if you're familiar with Linux shell) – RomanPerekhrest Dec 18 '15 at 17:53

1 Answers1

0

You can do something like this:

// your code

$possible_extensions = array("jpg", "jpeg", "png", "gif");
$row = array_combine($columns, $data);
foreach($possible_extensions as $ext){
    $filename = $row['row4'] . "." . $ext;
    if (file_exists("$rootDir/$filename")) {
        $row['image'] = .$filename;
        $row['small_image'] = .$filename;
        $row['thumbnail'] = .$filename; 
        break;
    }
}
fputcsv($newFile, array_values($row), ";",'"' );

// your code

Edited:

If you want to perform case-insensitive file_exists() check then here's the solution,

The following fileExists() function returns the full path file if found, and false if not.

function fileExists($fileName, $caseSensitive = true) {

    if(file_exists($fileName)) {
        return $fileName;
    }
    if($caseSensitive) return false;

    // Handle case insensitive requests            
    $directoryName = dirname($fileName);
    $fileArray = glob($directoryName . '/*', GLOB_NOSORT);
    $fileNameLowerCase = strtolower($fileName);
    foreach($fileArray as $file) {
        if(strtolower($file) == $fileNameLowerCase) {
            return $file;
        }
    }
    return false;
}

Here's the source:

And now your code,

// your code

$possible_extensions = array("jpg", "jpeg", "png", "gif");
$row = array_combine($columns, $data);
foreach($possible_extensions as $ext){
    $filename = $row['row4'] . "." . $ext;
    if ($filename = fileExists("$rootDir/$filename", false)) {
        $row['image'] = .$filename; //also needs correct extension of image which exists.
        $row['small_image'] = .$filename;
        $row['thumbnail'] = .$filename; 
        break;
    }
}
fputcsv($newFile, array_values($row), ";",'"' );

// your code
Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • Mmmm... what about files with the extension `.JPG` rather than `.jpg` and other variations of case. – Mark Setchell Dec 18 '15 at 14:19
  • This is a bit expensive because you are looping through every filename four times. It would be better to look at each filename, then run a loop checking for extension until it finds the right one. At that point you'd only run through each four times if they were all the extension that was last in your array. – Jesse Williams Dec 18 '15 at 14:23
  • @MarkSetchell hmm, in that case it would be expensive because of the array size. – Rajdeep Paul Dec 18 '15 at 14:24
  • 1
    @JesseWilliams Not every time. Once it gets the file it will do it's operation and then break out from the loop, hence the `break` statement. – Rajdeep Paul Dec 18 '15 at 14:26