Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel4 unable to create a directory although I gave correct permissions

Laravel4 unable to create a directory although I gave correct permissions. FileException error happened, http://s28.postimg.org/xkor8srxp/2014_01_03_2_53_18.png. I tried lots of permissions..

  • sudo chmod -R 777 storage
  • sudo chmod -R 707 storage
  • sudo chmod -R o+rwx storage

and I even restarted Apache server. I have been googling 24 hours or so, and still can't figure out.. Here are the routes and view files.

// routes.php

Route::post('handle-form', function()
{
    $name = Input::file('book')->getClientOriginalName();
Input::file('book')->move('/storage/directory', $name);
return 'File was moved.';
});

// form2.blade.php
<form action="{{ url('handle-form') }}"
  method="POST"
  enctype="multipart/form-data">

<input type="file" name="book" />
<input type="submit">
</form>

Currently what I did to the routes.php by following your help,

Route::post('handle-form', function()
{
$name = Input::file('book')->getClientOriginalName();
Input::file('book')->move(public_path().'/storage/directory', $name); 
return 'File was moved.';
});

and the permission I gave was sudo chmod -R 777 storage

like image 872
seoyoochan Avatar asked Jan 03 '14 23:01

seoyoochan


1 Answers

This is what you can do to make it work:

Create a directory in your public folder, or any other place:

 mkdir -p /var/www/your-site-path/public/storage/directory

Make it writable by your webserver

sudo chmod 770 /var/www/your-site-path/public/storage/directory

sudo chown username:www-data /var/www/your-site-path/public/storage/directory
or
sudo chown dongju:httpd /var/www/your-site-path/public/storage/directory

Tell Laravel to write the file to that folder this way:

$name = Input::file('book')->getClientOriginalName();

Input::file('book')->move(public_path().'/storage/directory', $name);    

Using this method I create here those two routes and it worked for me:

Route::get('form', function() {
    return 

        Form::open(array('url' => 'http://your-dev-server-address/upload', 'files' => true)) .

        Form::file('book', $attributes = array()) .

        Form::submit('submit') .

        Form::close();
});

Route::post('upload', function() {

    Input::file('book')->move(public_path().'/storage/directory', Input::file('book')->getClientOriginalName());

});

The first one /form will show you a file upload form and the second one /upload will get the file and save it to the correct path.

like image 58
Antonio Carlos Ribeiro Avatar answered Nov 16 '22 19:11

Antonio Carlos Ribeiro