2

I'm using PHP to check if a file exists and then get it's size.

The code worked great before but now that we're using a UNC path I can check that the path exists with file(exists($filename)) but when I try running exec("getsize" . $filename, $out); it tries to run for about a minute and then it returns that "The system cannot find the file specified". The user running this is currently an admin, otherwise I would think it is a permissions issue, but I'm not sure what else could be the problem if it is finding the file with file_exists() but then the exec() fails.

Any help or points would be greatly appreciated, thank you!

Code example:

<?php

$filename = "\\\\server\\share\\file_path_with_folders\\3019-74 (2).zip"; //Example file

if(file_exists($filename)){
    echo "File Exists: " . $filename . "\r\n";  
    // "File Exists: " . $filename" are getting echoed out, so it is succeeding
} else {
    echo "File doesn't exist: " . $filename  . "\r\n";
}

exec("getsize" . $filename, $out); //Runs command line command
//Getting "The system cannot find the file specified" error
echo "Out: " . $out[0] . "\r\n";
//Echos "Out: " and nothing else


?>
mario
  • 1,503
  • 4
  • 22
  • 42

1 Answers1

2

When in doubt, run Process Monitor.

Seriously, though, you should monitor the process with your favourite file-access-monitoring utility.

I would have to hazard a guess, but the space in the filename may be causing your problem. You may need to surround $filename with a set of quotes when calling getsize.

When assembling a command to pass to exec(), you also need a space in between a command and the parameters for that command. (This script produces the equivalent of "getsizetest.txt" instead of "getsize test.txt")

You should really run these commands in an interactive PHP shell on your local machine, if possible.

EKW
  • 124
  • 2
  • 9
  • I thought you had it with the quotes but I added surrounded $filename with double quotes and it didn't help. I'm not sure I'll be able to run that on the server, have to talk to the server guys, but I will definitely keep that in mind. Thanks @EKW! – mario Mar 26 '15 at 21:07
  • 1
    @gv0000 Added a note about another bug I noticed this morning, and I highly suggest using the PHP interactive shell (which is what made me notice the other bug... if I'd followed my own advice to begin with...) – EKW Mar 30 '15 at 16:02