-2

I have a bit of PHP code for searching the first 'column' of a CSV file for a filename, and if it's found it 'removes' it into a 'removed' folder.

There must be something wrong with my code because if (in_array is not seeing a name in the array which is identical to $fileNoExtension. I added the line at the bottom to check, and I get echo'd back to me "[ImportantFile] apparantly is not the same as [ImportantFile]???"

I've taken the code out which gets fileNoExtension from $thisFileWithExtension just so it's not in the way.

foreach ($fileArray as $FileWithExtension) {
    if (file_exists('PHP/ManualRemove.csv')) {
        $csvManualDemos = fopen("PHP/ManualRemove.csv", "r");
        while ($csvRows = fgetcsv($csvManualRemove)) {
            if (in_array($fileNoExtension, $csvRows)) {
                echo "Found on manual remove list. Removed.";
                rename("$fileWithExtension", "Removed/$fileWithExtension");
                continue;
            } else
                echo "\n [$fileNoExtension] apparantly is not the same as [$csvRows[0]]???\n";
        }
    }
}

I'm sure i've got some code wrong somewhere but I can't work it out! Many thanks :D

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
UnluckyForSome9
  • 301
  • 1
  • 9
  • 1
    `$csvManualDemos`; is that a typo? Should be `$csvManualRemove` maybe? – Guillaume Boudreau Aug 18 '18 at 22:15
  • Please add to your code an example of what the variable `$fileNoExtension` contains, and what the `PHP/ManualRemove.csv` contains (namely, just the row that should contain `$fileNoExtension`. – Guillaume Boudreau Aug 18 '18 at 22:17
  • Yes. Better to know what is coming out of your CSV file and what is $FileWithExtenstion is good. – Rinsad Ahmed Aug 18 '18 at 23:04
  • Apologies for my typos, they occured when I edited my code for posting here, I assue you there are no typos! I've discovered the (in_array) is only missing the 1st row, the rest of the time it can see what's there. Any reason why it's doing this? – UnluckyForSome9 Aug 19 '18 at 08:25

2 Answers2

1

Apologies for the typo's in my code, guys - they were from editing the code to post here rather than errors in my actual code.

I found the answer to my problem. For some reason in_array was missing the first row of my CSV. I had saved my CSV as "CSV UTF-8" instead of just "CSV" which was causing this problem.

UnluckyForSome9
  • 301
  • 1
  • 9
0

You have a case sensitivity issue in your code. You have mixed up $FileWithExtension and $fileWithExtension. They are not the same.

Rinsad Ahmed
  • 1,877
  • 1
  • 10
  • 28