1

I have these errors when running my script.

failed to open dir: No such file or directory in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6

Warning: scandir(): (errno 2): No such file or directory in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6

Warning: array_diff(): Argument #1 is not an array in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6

Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 7

Well this is my code below, I don't understand why it failed to open my dir when it's being declared below? Can someone help me with this.

Code of my word2.php

<?php
$numargs = count($argv);
if ($numargs > 1) {
    $folder = $argv[1];
    echo "Folder is: " . $folder . "\n";
    $files = array_diff(scandir($folder), array('.', '..')); //line 6
    foreach ($files as $file) { //line 7
        $filename = str_replace("í»", "", $filename);
    }
} else {
    echo "You need to pass the folder absolute path";
    exit();
}

Code for running my script using this command ./run.bat this is the filename with a code below.

php word2.php "/Applications/MAMP/htdocs/sites-store/word/images"

PAUSE
cachess
  • 163
  • 1
  • 5
  • 18
  • 1
    I thing the error message is quite clear, no? The `$folder` variable doesn't contain a valid path. Have you done any debugging, like dumping that variable to see what it contains? How do you call this script? From the command like? With which parameters? What does your folder structure look like? – M. Eriksson May 11 '18 at 06:30
  • Make sure that the requested directory is exists. `Array_diff()` make sure you put the array for first argument. Make sure that you iterating an array in `foreach`. – Salim Ibrohimi May 11 '18 at 06:32
  • I have updated my questions when supporting code above have a look. @MagnusEriksson, yes I'm wondering why the path from `run.bat` is not working. – cachess May 11 '18 at 06:45

2 Answers2

0

failed to open dir: No such file or directory in /Applications/MAMP/htdocs/sites-store/word/word2.php on line 6

  1. Make sure that your file in that directory.
  2. Check the filename again means is there any spelling mistakes or duplicated.
  3. Make sure if it's read that directory or not
Arun
  • 1,933
  • 2
  • 28
  • 46
Avinash
  • 3
  • 2
0

Try changing your .bat file to

php word2.php -- "/Applications/MAMP/htdocs/sites-store/word/images"

PAUSE

and you should also do var_dump($argv), to see how your script gets its parameter.