Console - access files

Hello everyone,
I read the book (https://ebook.odan-consulting.com/slim4/articles/console) on how to create an order via console - it works fine.
However, I have some issues opening/retrieving a file.

When I open the file (dirname(FILE).‘/…/…/…/public/files/’.$event[‘file’]), I get an error => Failed to open stream: No such file or directory.
If I check with file_exists everything is fine.

I checked that the user who accesses the file is indeed the owner of the file.

Any idea how to solve this problem?

Thanks

Hi @stef157

The “Failed to open stream: No such file or directory” error typically occurs when PHP cannot find the file you are trying to open at the specified path, but maybe there are other issues preventing you from opening it.

You can use __DIR__ to get the current directory of the script and then build the absolute path from there. Check the Path: Make sure that the path you are using to open the file is correct.

$file = __DIR__ . '/../../public/files/' . $event['file'];

Ensure that the file has the correct permissions that allow the user running the PHP script to read it. You can use chmod to modify file permissions.
Also double-check that the user executing the PHP script is the owner of the file and has the necessary permissions.

Hi,
Yes really weird.

When I print __DIR__, I get : /web/src/Console/Events
My file is in the folder public/files (/web/public/files)

My variable is also create as you write it :
$file = __DIR__.'/../../../public/files/'.$event['file'];

This test give me “FILE IS OK” :

if(file_exists($fileTmp)){
    $output->writeln('FILE IS OK');
}

When I print $user = posix_getpwuid(posix_geteuid());, I get :

Array
(
    [name] => sys_eventsDEV_23
    [passwd] => x
    [uid] => 5074
    [gid] => 5007
    [gecos] => 
    [dir] => /home/sys_eventsDEV_23
    [shell] => /bin/bash
)

When I ls -l /web/src/Console/Events/../../../public/files/, I get :
-rw-r--r-- 1 sys_eventsDEV_23 client2 1839114 Nov 21 17:04 IMG_9228.jpg
=> I

And, when I try to open the file, I get this :
Warning: fopen(/web/src/Console/Events/../../../public/files/IMG/9228.jpg): Failed to open stream: No such file or directory

Did you notice a mistake ?
Thanks

The only different I spot is the path and the filename.

IMG/9228.jpg <> IMG_9228.jpg

1 Like

You’re totally right… I use the pCloud SDK to upload the file on it… and when I look into the code, here is what I see… so, I’ll manage it.

Thanks for it

$path = str_replace(array("\\", "_"), DIRECTORY_SEPARATOR, $path);