Search code examples
phplaravellaravel-4image-uploading

Can't upload images to a public directory in laravel


I'm trying to upload images into a Laravel app that will then need to be publicly displayed. However, I seem to only be able to upload to a folder within the application root. Any attempt to upload to the public directory throws the following error:

protected function getTargetFile($directory, $name = null)
{
    if (!is_dir($directory)) {
        if (false === @mkdir($directory, 0777, true)) {
            throw new FileException(sprintf('Unable to create the "%s" directory', $directory));

I'm assuming this means that Laravel is going to prevent me from uploading to any file that is publicly accessible. I actually rather like this, however, how can I link to images that are outside of the public directory I've tried to ../ my way out of the public folder, but appropriately, that didn't work.

Alternately, anything that will let me uploaded directly to the public folder would be great too.

If it helps, here is my controller method that puts the file in public folder:

$thumbnail_file = Input::file('thumbnail'); 
$destinationPath = '/uploads/'. str_random(8);
$thumbnail_filename = $thumbnail_file->getClientOriginalName();
$uploadSuccess = Input::file('thumbnail_url')->move($destinationPath, $thumbnail_filename);

Solution

  • Your destination path should be:

     $destinationPath = public_path(sprintf("\\uploads\\%s\\", str_random(8)));