csv icon indicating copy to clipboard operation
csv copied to clipboard

CreateFromPath doesn't error when given a directory

Open pleaseremove opened this issue 7 years ago • 1 comments

Feature Request

Q A
New Feature yes
BC Break no

Proposal

When constructing a createFromPath based reader it checks to see if there is no resource on an fopen, but fopen returns a resource on a directory too, so if you fail to pass in a filename and just a directory this exception is not thrown. Would be nice if it could check it has a file at that path.

if (!$resource = @fopen(...$args)) { throw new Exception(sprintf('%s: failed to open stream: No such file or directory', $path)); }

Thanks

pleaseremove avatar Jun 27 '19 10:06 pleaseremove

@pleaseremove thanks for using the library. While I understand your concern, there's a reason why the check on path is not done as you ask because createFromPath is not limited to local path It can perfectly use remote file over the internet using the $context optional argument. And is_dir can be restricted by the server configuration which can lead to other warning and notice.

IMHO the check to see if you are using a file vs a directory should be made prior to using the named constructor. See below:

<?php

if (file_exists($path) && !is_dir($path) {
   Reader::createFromPath($path); 
}

This is a lot cleaner and you don't coerce the library in becoming what it is not (ie: a path validation library which is out of scope of its responsibilities).

nyamsprod avatar Jun 27 '19 20:06 nyamsprod