0

First please excuse any grammar or spelling mistakes. English is my second language and it's 2AM at the time of writing this. I was trying to create a php script that can be executed in powershell, which from inside a hyper-v virtual machine. This script would take files from a drive which is shared with the virtual machine and hashes these files.

I followed this guide for accessing the shared drive. This guide for executing php scripts in powershell. For that you need to set these environment variables.

The below code is what I am trying to execute and it is located at: \\tsclient\E\My_Projects\new_code\hashing_script.php

$directory = "\\tsclient\E\My_Projects\new_data";
$directoryContents = scandir("$directory");
$counter = count($directoryContents, 0);
$hashes = [0, 0];
for($i = 2; $i < $counter; $i++)
{
$hash = hash_file("sha512", "$directory\$directoryContents[$i]");
array_push($hashes, $hash);
}
var_dump($hashes);

when I execute the code in powershell with the command: php.exe \\tsclient\E\My_Projects\new_code\hashing_script.php I get the following error message:

PHP Warning: scandir(\tsclient\E\My_Projects
ew_data, \tsclient\E\My_Projects
ew_data): Das System kann den angegebenen Pfad nicht finden. (code: 3) in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 2

Warning: scandir(\tsclient\E\My_Projects
ew_data, \tsclient\E\My_Projects
ew_data): Das System kann den angegebenen Pfad nicht finden. (code: 3) in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 2
PHP Warning: scandir(\tsclient\E\My_Projects
ew_data): failed to open dir: No such file or directory in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 2

Warning: scandir(\tsclient\E\My_Projects
ew_data): failed to open dir: No such file or directory in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 2
PHP Warning: scandir(): (errno 2): No such file or directory in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 2

Warning: scandir(): (errno 2): No such file or directory in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 2
PHP Warning: count(): Parameter must be an array or an object that implements Countable in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 3

Warning: count(): Parameter must be an array or an object that implements Countable in \\tsclient\E\My_Projects\new_code\hashing_script.php on line 3
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(0)
}

Das System kann den angegebenen Pfad nicht finden

is german and roughly translates to:

The system can not find the path specified

The two main things that I have noticed about the error messages are that:

  1. when it returns the address path one of the backslashes is missing
  2. it interperates the folder name as \new_code as \n and continues on the next line.

To fix this I have:

  1. run other test files that just echo data and they work excelently.
  2. tried reading the dir name from a .txt file. This returned an error message that it was unable to find the .txt file.
  3. tried using $_SERVER['DOCUMENT_ROOT'] this did not change a lot in the error message.

I have come to the conclusion that it almost seems that powershell/php thinks that \tsclient\E\My_Projects\new_code\hashing_script.php is a directory and is trying to search inside of it, which obviously won't work.

Thank you for taking your time to read this any and all suggestions/ideas/questions are welcome.

1 Answer 1

1

You need to be in the habit of protecting your backslashes. Your string contains \n, which of course translates to a newline, not to the two characters you expect. You either need to double the backslashes or use single quotes:

$directory = "\\\\tsclient\\E\\My_Projects\\new_data";
$directory = '\\tsclient\E\My_Projects\new_data';
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.