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

rhand's avatar
Level 6

PDF fails to be uploaded in Laravel Media Manager

We are using Laravel Media Manager https://github.com/ivenms/laravel-media-manager for our new Laravel 9 set up and worked out most kinks. But issue we are having stil is that it seems we cannot upload pdf files. This is not the case on Laravel 8 setup with the same uploadController. We do allow them during validation but somehow they do not get stored. I can see in the upload request that pdf with base 64 encoded data is uploaded

  • upload:json[{"success":true,"file_name":"iphone-intro.pdf"}]
  • data: application/pdf with pdf data shown with base 64 encoded data in request header but no response
  • upload-image {"success":true,"data":[]} with Request URL: https://site.test/editor/upload-image

which triggers route

Route::post('upload-image', [Editor\UploadController::class, 'uploadImage']);

and no pdf stored

Here is the method dealing with the basis of our uploads in UploadController.php:

...
public function uploadImage(Request $request)
{
    if (! $request->file('file')->isValid()) {
        return $this->respondFailedWithError(400, 'The file is invalid.');
    }

    if ($request->file('file')->getSize() > 26214400) {
        return $this->respondFailedWithError(400, 'The file is max size 25MB.');
    }

    if (! isset($this->currentProject)) {
        return $this->respondFailedWithError(400, 'The session has expired and current project can not be selected.');
    }

    if (! $this->currentProject->isAllowUpload()) {
        return $this->respondFailedWithError(400, "The storage is full. Can't upload.");
    }

    $originalName = pathinfo($request->name, PATHINFO_FILENAME);

    $originalExt = pathinfo($request->name, PATHINFO_EXTENSION);

    $sizes = [3200, 1600, 1200, 700, 350];

    foreach ($sizes as $size) {
        if (array_search($originalExt, ['mp4', 'svg', 'tiff', 'pdf']) !== false) {
            break;
        }
....

and here MediaController.php part (our custom controller overruling Media Manager's) taking care of uploading files:

...
/**
     * upload new files.
     *
     * @param Request $request [description]
     *
     * @return [type] [description]
     */
    public function upload(Request $request)
    {
        $this->validate($request, [
            'file.*' => 'required|mimes:mp4,ogx,oga,ogv,ogg,webm,application/octet-stream,audio/mpeg,mpga,mp3,wav,jpg,jpeg,png,bmp,svg,pdf',
        ]);

        $upload_path = $request->upload_path;
        $random_name = filter_var($request->random_names, FILTER_VALIDATE_BOOLEAN);
        $result = [];
        $broadcast = false;
        $custom_attr = collect(json_decode($request->custom_attrs));

        foreach ($request->file as $one) {
            if ($this->allowUpload($one)) {
                $one = $this->optimizeUpload($one);
                $orig_name = $one->getClientOriginalName();
                $name_only = pathinfo($orig_name, PATHINFO_FILENAME);

                $ext_only = pathinfo($orig_name, PATHINFO_EXTENSION);
                $final_name = $random_name
                                ? $this->getRandomString()
                                : $this->cleanName($name_only);

                $final_name = Str::slug($final_name).".$ext_only";

                $file_options = optional($custom_attr->firstWhere('name', $orig_name))->options;
                $file_type = $one->getMimeType();
                $destination = ! $upload_path ? $final_name : $this->clearDblSlash("$upload_path/$final_name");

...

Any ideas why this would no longer work now?

0 likes
2 replies
rhand's avatar
Level 6

I see what happens now. All pdfs and some pngs (original files perhaps ) as well go to public/uploads and not to the project directory. Looking into this some more now.

rhand's avatar
rhand
OP
Best Answer
Level 6

Got upload path updated

- $upload_path = $request->upload_path;
+ $upload_path = $this->currentProject->id . $request->upload_path;

Please or to participate in this conversation.