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

ajck's avatar
Level 1

How to get correct path to an uploaded image file to pass to a PHP function?

I'm trying to access an image file (uploaded with AJAX) to a Laravel 5.4 backend, and having trouble getting native PHP functions to access it. File is uploading fine (appears in the correct upload folder) but Laravel error reports that PHP's getimagesize() can't find the file) Actual error is:

(1/1) ErrorException
getimagesize(storage/public/images/viE9JvY8jfA4fVuZL7vkh3RdZY0zyT4svH4Xldv1.jpeg): failed to open stream: No such file or directory

Partial code is below:

public function imgupload(Request $request)
    {
    // Store file from 'imgfile' field from uploaded form (imguploadform in blade template) in /storage/app/public/images/
        $path = $request->file('imgfile')->store('public/images');
    // Generate a thumbnail from the image using PHP's inbuilt GD library and store in thumbs dir:;     
    $thumbpath = $this->makethumbs($path);
        
        return response()->json(['thumb' => asset($thumbpath)]); // Return path 
    }

// Reads full size uploaded image from storage, generates a smaller thumbnail and saves it to the thumbs/ dir
public function makethumbs($img, $maxwidth = 120, $maxheight = 120)
    {
    $img = 'storage/app/'.$img;
    $arr_image_details = getimagesize($img);

    /* Other stuff to generate thumbnail here */

    // Write thumbnail image out to thumbs/ folder

    return($thumbnail_path);
    }

This is on my local Windows 7 laptop (tho will need to work online later, probably Heroku). I'm running on Bitnami's WAMP stack, and root Laravel folder is apache2/htdocs/seg . In there, public/ contains a folder link called 'storage' which maps to storage/app/ and that folder contains public/ then in there folders images/ and thumbs/ . So intention is to read uploaded image out of images/ then resize and put into /thumbs.

I did a few Log:info()'s and found $path (in my imgupload func) is: public/images/viE9JvY8jfA4fVuZL7vkh3RdZY0zyT4svH4Xldv1.jpeg

and in makethumbs() PHP's current working dir (via getcwd()) is: C:\Bitnami\wampstack-7.1.2-0\apache2\htdocs\seg\public

So if getimagesize() is starting from PHP's working dir, in theory it should be looking in: C:\Bitnami\wampstack-7.1.2-0\apache2\htdocs\seg\public/storage/app/public/images/[filename]

Any ideas welcome!

0 likes
4 replies
jlrdw's avatar

So if getimagesize() is starting from PHP's working dir, in theory it should be looking in: C:\Bitnami\wampstack-7.1.2-0\apache2\htdocs\seg\public/storage/app/public/images/[filename]

NO, even in development you should have main laravel above htdocs.

Like

apache2
-----segup   /////MAIN LARAVEL IN THIS FOLDER
-----htdocs
----------seg
------------public stuff under seg
------------index.php
------------.htaccess
------------assets
-----------------js
------------------etc

Then you could also use asset helper.

Sorry my "folder" drawing isn't that good.

ajck's avatar
Level 1

@jlrdw by asset helper do you mean asset() function?

Laravel is installed where composer put it - as a newbie to Laravel I just trusted it I have to say, and no idea how to move everything now...

How/where would I use asset() in my code above?

Thanks

jlrdw's avatar

https://laravel.com/docs/5.4/helpers

You can, after a composer install move things correctly even in development following this:

http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/

I also use bitnami, just did a 5.4 install, prior I used 5.1.

Just good to get used of a correct install weather dev or production.

If you have more question on install, start new post. But I'd get it installed correctly before proceeding.

Edit:

also see https://laravel.com/docs/5.4/filesystem

and

https://laravel.com/docs/5.4/requests#files

I'm trying to access an image file (uploaded with AJAX) to a Laravel 5.4 backend, and having trouble getting native PHP functions to access it.

But, what are you trying to do / achieve with file?

Oh, see https://stackoverflow.com/questions/40025456/image-intervention-w-laravel-5-4-storage

Please or to participate in this conversation.