Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

WimN's avatar
Level 5

File not found on ubuntu server

Hello,

On my local windows environment both tries do work. Loading a jpg with a fixed name and with a variable name. On my Ubunto server however the jpg with the variable name $nfdcode is saved but not loaded.

//My goal:
 Storage::put('public/nfds/'.$nfdcode.'.jpg', $encoded);
 return redirect('storage/nfds/'.$nfdcode.'.jpg');          
      
//A try with fixed text:
Storage::put('public/nfds/test.jpg', $encoded);
return redirect('storage/nfds/test.jpg);

When I encode the $nfdcode to a fixed text eg :

$nfdcode = "test";

Both Windows and Ubunto do the job. But variable codes with 9 characters numbers and letters do not work on Ubuntu. An LS of the directory show that the files were made and saved:

root@localhost:/var/www/ourapp/storage/app/public/nfds# ls
EAwlEWWbHL.jpg  test.jpg

Thanks for your help!!

0 likes
4 replies
LaryAI's avatar
Level 58

It seems like you're encountering an issue with file access on your Ubuntu server. The fact that the file is being saved correctly but not loaded suggests that there might be a problem with file permissions or with the way the file path is being resolved.

Here's a checklist to help you troubleshoot the issue:

  1. File Permissions: Ensure that the web server has the necessary permissions to read the files in the storage/app/public/nfds directory. You can set the correct permissions using the chmod command. For example, to give read permissions to all users, you could use:
chmod -R 755 /var/www/ourapp/storage/app/public/nfds
  1. Symbolic Link: Laravel's public/storage directory is typically a symbolic link to storage/app/public. Make sure that this symbolic link exists and is correctly pointing to the storage directory. You can create the link using the php artisan storage:link command.

  2. File Paths: Ensure that the file paths are being constructed correctly. It's possible that there's a difference in how paths are handled between Windows and Ubuntu. Double-check that the $nfdcode variable does not contain any characters that might be invalid or handled differently on a Unix-based file system.

  3. URL Encoding: If the $nfdcode contains special characters, they may need to be URL-encoded when constructing the path for the redirect. You can use PHP's urlencode() function to ensure that the filename is properly encoded for a URL.

  4. Filesystem Configuration: Check your config/filesystems.php to ensure that the public disk is configured correctly and that the root path is pointing to the correct directory.

  5. Debugging: Add logging to your code to ensure that the $nfdcode variable contains the expected value just before the file is saved and before the redirect is performed.

Here's an example of how you might modify your code to include some of these checks:

// Ensure that $nfdcode is a valid filename and URL-encode it
$nfdcode = urlencode($nfdcode);

// Save the file
Storage::put('public/nfds/'.$nfdcode.'.jpg', $encoded);

// Check if the file exists before redirecting
if (Storage::exists('public/nfds/'.$nfdcode.'.jpg')) {
    // Redirect to the file
    return redirect('storage/nfds/'.$nfdcode.'.jpg');
} else {
    // Log an error or handle the case where the file doesn't exist
    Log::error("File not found: " . $nfdcode . '.jpg');
    // Return an appropriate response, such as a 404 error
    abort(404, 'File not found.');
}

Make sure to replace Log::error with the appropriate logging mechanism if you're not using Laravel's built-in logging.

If you've gone through all these steps and the issue persists, you may need to provide more information about the server configuration, the exact error messages you're receiving, and any other relevant details that could help diagnose the problem.

WimN's avatar
Level 5

I tried all suggestions made by Larry without result.

The if exists test returns true but still the redirect does not work. If false there should be an error in the log.

I tried creating a new string variable as name and save it first in the database with the same result. "404"

If the problem are File Permissions than why is the hard coded variable $nfdscode "test.jpg" redirecton and the random string variable not? Both have the same permissions :

-rw-r--r-- 1 www-data www-data 338781 Dec  4 10:38 BdDXKegGSucyU.jpg
-rwxr-xr-x 1 www-data www-data 333189 Dec  4 09:41 PkG1JTXGzR.jpg
-rwxr-xr-x 1 www-data www-data 329275 Dec  4 09:38 test.jpg
-rwxr-xr-x 1 www-data www-data 329686 Dec  4 10:28 yAEAKZrzOrCYn.jpg

All files have read rights for all groups. BdDXKegGSucyU.jpg and yAEAKZrzOrCYn.jpg both result in a 404 not found.

Thanks for every suggestion !

DhPandya's avatar

@WimN Check if you are not running in the case-sensitive issue. This runs on your windows but not on Ubuntu means something can be related to case issues.

WimN's avatar
Level 5

@DhPandaya Thanks for your reply. But since I'm passing the variable around once created, I fear the problem is really in the stringvalue that has been created as name for the file. A variable with hardcoded text works fine but a string created by randow function below not. The files are created but can not be returned.

//Works:
$nfdcode = "test"

//Does not work:
$nfdcode = Str::random (14);
//even after replacing numbers:
$this->text= Str::random(14);
$this->text = preg_replace('/[0-9]+/', '', $this->text);
//neither does:
$nfdcode= uniqid();

Thanks!

Please or to participate in this conversation.