Laravel 5.4部署至cpanel后Laravel Mediable路径问题求助
Hey there! Let's tackle this path problem you're running into with Laravel Mediable after deploying to cPanel. Since Intervention is successfully saving files but Mediable can't locate them to attach to your User model, the root cause is almost certainly how file paths are being handled between your local environment and the cPanel server. Here are the most likely fixes:
1. Switch to Absolute Paths Everywhere
Local development servers (like Valet or Artisan serve) often run from your project root, so relative paths like uploads/users/default/... work fine. But cPanel typically runs your app from the public_html directory, which changes the working directory—breaking relative paths.
Update your code to use Laravel's public_path() helper to generate absolute paths for both Intervention and Mediable:
protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); $id = $user->id; $uname = $user->name[0]; // Use public_path() for Intervention operations $defaultImgPath = public_path("uploads/users/default/{$uname}.png"); $userImgPath = public_path("uploads/users/images/{$id}.png"); $img = Image::make($defaultImgPath)->save($userImgPath); // Pass full absolute path to MediaUploader::import() $media = MediaUploader::import($userImgPath); $user->attachMedia($media, 'user'); // Repeat for the badge image $badgeDefaultPath = public_path("uploads/users/default/badge1.png"); $badgeImgPath = public_path("uploads/users/images/{$id}badge.png"); $img = Image::make($badgeDefaultPath)->save($badgeImgPath); $badgeMedia = MediaUploader::import($badgeImgPath); $user->attachMedia($badgeMedia, 'badge'); }
Note: Your original MediaUploader::import() call used split parameters (directory, subdir, filename, extension), but the correct usage expects a single absolute file path—this was likely another key reason it couldn't find the files.
2. Verify Directory Permissions on cPanel
Even if Intervention can save files, Mediable might lack proper read permissions to access them. In cPanel's File Manager:
- Navigate to
public/uploads - Right-click the directory and select Change Permissions
- Set permissions to
755for directories and644for files - Ensure the directory is owned by your cPanel username (not
root)
If you have SSH access, run these commands instead:
chmod -R 755 public/uploads chown -R your_cpanel_username:public public/uploads
3. Double-Check Laravel Filesystem & Mediable Configs
Make sure your filesystem and Mediable configurations point to the correct locations:
- In
config/filesystems.php, confirm thepublicdisk is rooted at your public directory:'public' => [ 'driver' => 'local', 'root' => public_path(), 'url' => env('APP_URL').'/public', 'visibility' => 'public', ], - In
config/mediable.php, set the default disk topublic:'default_disk' => 'public',
4. Debug the Exact Path Being Used
If you're still stuck, add debug code to log the paths Mediable tries to access:
$userImgPath = public_path("uploads/users/images/{$id}.png"); \Log::info("Attempting to import file: {$userImgPath}"); \Log::info("File exists? " . (file_exists($userImgPath) ? 'Yes' : 'No')); $media = MediaUploader::import($userImgPath);
Check your Laravel logs (storage/logs/laravel.log) to confirm the path is correct and the file exists.
These steps should resolve the path mismatch between your local environment and cPanel. Let me know if you run into any other snags!
内容的提问来源于stack exchange,提问作者Shahrukh Ijaz

