Wednesday, January 31, 2018

Upload photo PHP/Ajax - development

Upload photo PHP/Ajax

I’m trying to upload a photo with Ajax and PHP, but return an error, and I don’t know why:

message: “move_uploaded_file(/site/uploads/users/user.png): failed to
open stream: No such file or directory”

RETURN (FILE):

C:xampptmpphp2BAB.tmp

JQUERY:

$("#user-upload-photo").submit(function (){

        $("#user-upload-photo").ajaxSubmit({
            url: '/ajax/settings/upload',
            type: 'post',
            beforeSubmit: function(){
                $("#photo-loading").show();
                $("#photo-figure").hide();
            },
            success: function () {
                $("#photo-loading").hide();
                $("#photo-figure").show();
            }
        });

    return false;
});

This is the code that return the error:

Controller:

public function upload(){
    if(!empty($_FILES)):
        if(is_uploaded_file($_FILES['user-photo-input']['tmp_name'])):
            $srcPath = $_FILES['user-photo-input']['tmp_name'];
            $trgPath = '/site/uploads/users/' . $_FILES['user-photo-input']['name'];

            if(move_uploaded_file($srcPath, $trgPath)):
                return response()->json(['succcess' => '1', 'photo' => $trgPath]);
            endif;

        endif;
    endif;
}



from Laravel Questions and Answers https://laravelquestions.com/php/upload-photo-php-ajax/
via Lzo Media

No comments:

Post a Comment