0

I'm new to Laravel and I'm trying to upload images to my website using Storage API but laravel always returns "Forbidden" after I submitted my form.

Here's my form (add_company.blade.php) :

{!! Form::model($company = new App\Models\Setting\Organization\Company, ['method' => 'POST', 'action' => 'Setting\Organization\CompaniesController@store', 'files'=>true]) !!}

    <div class="form-group">
        {!! Form::label('CompanyCode', 'Company Code : ', ['class' => 'col-lg-3 col-md-3 col-sm-3 col-xs-3']) !!}

        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
            {!! Form::text('CompanyCode', $company->autoGenerateCode(), ['class' => 'form-control', 'readonly' => true]) !!}
        </div>

    </div>

    <div class="form-group">
        {!! Form::label('Name', 'Company Name : ', ['class' => 'col-lg-3 col-md-3 col-sm-3 col-xs-3']) !!}

        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
            {!! Form::text('Name', null, ['class' => 'form-control']) !!}
        </div>

    </div>

    <div class="form-group">
        {!! Form::label('Logo', 'Company Logo : ', ['class' => 'col-lg-3 col-md-3 col-sm-3 col-xs-3']) !!}

        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
            <span class="btn btn-default btn-file form-control">
                Browse {!! Form::file('Logo', ['class' => 'form-control', 'id' => 'logo']) !!}
            </span>
        </div>

    </div>

    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
        {!! Form::submit('Add Company', ['class' => 'btn btn-primary']) !!}
    </div>
{!! Form::close() !!}

Here's the store method in my controller (App\Http\Controllers\Setting\Organization\CompaniesController) :

public function store(CompanyRequest $request){
    if (Request::file('Logo')->isValid())
    {
        $file = Request::file('Logo');
        $extension = $file->getClientOriginalExtension();
        $newFilename = $request->CompanyCode . "_logo";
        Storage::disk('local')->put($newFilename . '.' . $extension,  File::get($file));

        $request->Logo = $newFilename;
    }

    Company::create($request->all());
    flash()->success('Company ' . $request->Name . ' Added.');
    return redirect('company');
}

Laravel doesn't give any error but always returns "Forbidden" every time I submit my form. I didn't change anything in the config/filesystems.php or in the public/.htaccess.

Please help, I've already read many posts about file upload in Laravel 5 but didn't find any answer. Thanks a lot!

EDIT :

I tried @MikeMiller suggestion by uploading the file to public folder and give permission to that folder, but laravel still returns "Forbidden". Where am I go wrong?

public function store(CompanyRequest $request){
    if (Input::file('Logo')->isValid())
    {
        chmod("/public/", 0755);
        chmod("/public/uploaded_images/", 0755);
        mkdir("/public/uploaded_images/" . $request->CompanyCode, 0755);

        $destinationPath = '/public/uploaded_images/' . $request->CompanyCode; // upload path
        $extension = Input::file('Logo')->getClientOriginalExtension(); // getting image extension
        $fileName = $request->CompanyCode . '_logo.' . $extension; // renaming image
        Input::file('Logo')->move($destinationPath, $fileName); // uploading file to given path

        $request->Logo = $fileName;
    }

    Company::create($request->all());
    flash()->success('Company ' . $request->Name . ' Added.');
    return redirect('company');
}

Here's my directory:

app
bootstrap
config
database
public
    - bower_components
    - css
    - fonts
    - images
    - script
    - uploaded_images
resources
storage
tests
vendor

Here's the last record of my xampp\apache\logs\access.log :

::1 - - [31/Jan/2016:18:29:17 +0700] "GET /favicon.ico HTTP/1.1" 200 30894 "http://localhost/hrmsystem/public/company" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"

::1 - - [31/Jan/2016:18:30:25 +0700] "POST /hrmsystem/public/company HTTP/1.1" 403 9 "http://localhost/hrmsystem/public/company/create" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"

::1 - - [31/Jan/2016:18:30:26 +0700] "GET /favicon.ico HTTP/1.1" 200 30894 "http://localhost/hrmsystem/public/company" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"

::1 - - [31/Jan/2016:18:33:15 +0700] "GET /hrmsystem/public/company/create HTTP/1.1" 200 8544278 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"

::1 - - [31/Jan/2016:18:33:15 +0700] "GET /hrmsystem/public/company/create HTTP/1.1" 200 8544278 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"

::1 - - [31/Jan/2016:18:33:17 +0700] "GET /hrmsystem/public/company/dist/img/user2-160x160.jpg HTTP/1.1" 404 14906 "http://localhost/hrmsystem/public/company/create" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"

::1 - - [31/Jan/2016:18:33:26 +0700] "POST /hrmsystem/public/company HTTP/1.1" 403 9 "http://localhost/hrmsystem/public/company/create" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36"

Here's the xampp\apache\logs\error.log :

[Sun Jan 31 14:16:55.341100 2016] [ssl:warn] [pid 6500:tid 404] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 31 14:16:55.419279 2016] [core:warn] [pid 6500:tid 404] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Sun Jan 31 14:16:55.528603 2016] [ssl:warn] [pid 6500:tid 404] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 31 14:16:55.591103 2016] [mpm_winnt:notice] [pid 6500:tid 404] AH00455: Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12 configured -- resuming normal operations
[Sun Jan 31 14:16:55.591103 2016] [mpm_winnt:notice] [pid 6500:tid 404] AH00456: Apache Lounge VC11 Server built: Jul 12 2015 10:56:48
[Sun Jan 31 14:16:55.591103 2016] [core:notice] [pid 6500:tid 404] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Sun Jan 31 14:16:55.606729 2016] [mpm_winnt:notice] [pid 6500:tid 404] AH00418: Parent: Created child process 7348
[Sun Jan 31 14:16:56.992613 2016] [ssl:warn] [pid 7348:tid 192] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 31 14:16:57.226989 2016] [ssl:warn] [pid 7348:tid 192] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Sun Jan 31 14:16:57.273864 2016] [mpm_winnt:notice] [pid 7348:tid 192] AH00354: Child: Starting 150 worker threads.

Here's the php_error_log file :

[31-Jan-2016 02:03:18 Asia/Jakarta] PHP Fatal error:  Class 'App\Http\Controllers\Auth\Session' not found in C:\xampp\htdocs\hrmsystem\app\Http\Controllers\Auth\SessionsController.php on line 78

[31-Jan-2016 04:09:32 Asia/Jakarta] PHP Parse error:  syntax error, unexpected '<' in C:\xampp\htdocs\hrmsystem\storage\framework\views\c57e76e02cd2fcbe254871f7ad79d55e on line 44

[31-Jan-2016 15:33:11 Asia/Jakarta] PHP Fatal error:  Class 'App\Models\Setting\Organization\Carbon' not found in C:\xampp\htdocs\hrmsystem\app\Models\Setting\Organization\Company.php on line 175
Maya Nastasya
  • 197
  • 2
  • 16
  • 1
    You need to set your permissions on the upload folder – Mike Miller Jan 31 '16 at 10:15
  • @MikeMiller what is the default upload folder? and how to set the permission? I've tried using PHP chmod() in another folder before, but didn't work – Maya Nastasya Jan 31 '16 at 10:41
  • 1
    You need to specify. Looks like you are trying to put into the web root which shouldn't be writable. You should add a folder to public and set permissions there. You will need to get to the Linux command line to set folder permissions – Mike Miller Jan 31 '16 at 10:45
  • @MikeMiller I'll try your suggestion, what's the best permission code for it? – Maya Nastasya Jan 31 '16 at 10:46
  • @MikeMiller I've tried your suggestion but didn't work. Please read my edited post. Thanks a lot :) – Maya Nastasya Jan 31 '16 at 10:56
  • 1
    Your issue is definitely permissions so you need to debug them and/or your path in the store function. Not much more to suggest – Mike Miller Jan 31 '16 at 11:07

1 Answers1

1

Your destination path is wrong...

$destinationPath = 'uploaded_images/' . $request->CompanyCode; // upload path

Your paths are in relation to index.php

Mike Miller
  • 3,071
  • 3
  • 25
  • 32
  • I changed the destinationPath as you said, but still returns "Forbidden" – Maya Nastasya Jan 31 '16 at 11:15
  • You will need to check your logs to see which folder you are being denied access to – Mike Miller Jan 31 '16 at 11:16
  • I can't find it in my laravel-2016-01-31.log file. I tried to change the permission to 0777 but still returns "Forbidden". I'm just guessing, but maybe the error is because of XAMPP I'm using ? – Maya Nastasya Jan 31 '16 at 11:23
  • Server log not application log – Mike Miller Jan 31 '16 at 11:24
  • Sorry didn't read properly. You are looking for error log not access log. Check xampp control panel it used to have a log viewer – Mike Miller Jan 31 '16 at 11:50
  • Added to the post, please have a look – Maya Nastasya Jan 31 '16 at 11:55
  • Nothing there. Can't help anymore I'm afraid. I might be on the wrong track but without a more verbose error than 'Forbidden' it's not possible to debug further – Mike Miller Jan 31 '16 at 12:03
  • Thanks for your time answering my questions :D – Maya Nastasya Jan 31 '16 at 12:08
  • 1
    No probs. Keep messing it will be something simple. I would dump xampp and move to vagrant you will get a much more consistent behaviour and you will be on same stack as 90% of the laravel community ( and your prod hosting if you use forge) – Mike Miller Jan 31 '16 at 12:12
  • Hi @Mike , I found out that I forgot to return true in authorize function in my Request class. But now it returns error in chmod saying no such file or directories. `chmod("\\public", intval(0777, 8));` – Maya Nastasya Jan 31 '16 at 12:52
  • Your path is wrong then. Remember it's referenced from index.php which is in the public folder. You shouldn't change perms on public anyway just the upload dir – Mike Miller Jan 31 '16 at 12:55