quitano's avatar

Public Path Upload image Issue

 $file = Input::file('photo');

        $file = $file->move(public_path().'/uploads/', time(). '-' . $file->getClientOriginalName());

        $post->photo = $file->getRealPath();

Everything is working however its putting the full path into the database:

/var/www/domian.com/laravel/public/uploads/1421954572-photo.jpg

i need this: /uploads/1421954572-photo.jpg

thanks

0 likes
6 replies
RachidLaasri's avatar
    $file = Input::file('photo');

    $filename = 'uploads/', time(). '-' . $file->getClientOriginalName();

        $file = $file->move(public_path().'/'.$filename);

        $post->photo = $filename;
2 likes
quitano's avatar

The above code is creating a folder within the uploads directory. The folder that is being created is the name of the photo. For example 1421958765-photo.jpg is the name of the directory and storing a file in that folder called "phpZUSJI". Then when i go to the url path of the image like domain.com/uploads/1421958765-photo.jpg its stating -> This webpage has a redirect loop.

when i look at the string in my DB table its: uploads/1421958765-photo.jpg w/o (/) in front of the path should be /uploads/1421958765-photo.jpg

RachidLaasri's avatar

Try this :

$file = Input::file('photo');

$filename = time(). '-' . $file->getClientOriginalName();

$file = $file->move(public_path().'/uploads/', $filename);

$post->photo = '/uploads/'.$filename;
1 like
kumar's avatar

hi when i use post method for image upload i got the following error

my ajax code is;

$(document).ready(function (e) { $("#uploadForm").on('submit',(function(e) { e.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: "upload", type: "post", data: formData, contentType: false, cache: false, processData:false, success: function(data) { $("#targetLayer").html(data); }, error: function() { } }); })); });

my route;

Route::any('upload','ImageUploadController@Upload');

in route if use post i get this error:error:MethodNotAllowedHttpException in RouteCollection.php line 207:

while i use any or get i got this error:Call to a member function getClientOriginalName() on a non-object

what will i do?

kamleshcgtechno's avatar

Hi Rachid, I am using Lumen for api and my some apis have file uploading feature. Could you please suggest me how can we upload file in Lumen by api. BTW i have tried your code to upload image but system says 'public_path()' is not defined. I did not find where is public_path() defined and how can i use it in a controller to upload an image in public folder? kindly share your suggestion. Thanks a lot :)

Please or to participate in this conversation.