php + jquery上传图片后将文件夹移到图片

时间:2019-02-24 16:10:59

标签: php jquery image upload

我有问题。

图像上传完成后,我想用用户名将图片移动到头像文件夹中。

profile.php

<div class="upload"><i class="fas fa-camera"></i></div>
<form>
<input name="avatar" type="file" id="avatar" accept="image/png,image/gif,image/jpeg" />
</form>

function _(el) {
  return document.getElementById(el);
}

function uploadavatar() {
        var file = _("avatar").files[0];
        var formdata = new FormData();
        formdata.append("image", file);
        var ajax = new XMLHttpRequest();
        ajax.addEventListener("load", completeHandler, false);
        ajax.open("POST", "<?=$base?>upload.php");
        ajax.send(formdata);
        $(".upload").html("<i class=\"fas fa-spinner fa-pulse\"></i>");
}


function completeHandler(event) {
    $.ajax({
        url: "<?=$base?>check.php", 
        type: "POST",             
        data: { 'p': 'avatar', 'img': event.target.responseText},
        success: function(data) {
            alert(data);
            $(".upload").html("<i class=\"fas fa-camera\"></i>");
        }
    });
}

加载图像后,我想在后期处理中移动图像并将其保存到数据库中

图像已上传,但file_exist找不到图像

check.php

 if(file_exists($base.'image/upload/'.$_POST['img']))
                    {
                        $imagename=substr($_POST['img'], 0, strlen($_POST['img'])-4 );
                        $imageext=substr($_POST['img'], -4);
                        $image=$imagename.$imageext;
                        rename($base.'image/upload/'.$image, $base.'image/upload/'.$_COOKIE["username"].$imageext);
                        $image=$_COOKIE["username"].$imageext;
                        $thumb=thumbnail($base.'image/upload/'.$image,$base.'image/avatar/'.$_COOKIE["username"].$imageext,80);
                        echo $base.'image/avatar/'.$_COOKIE["username"].$imageext;
                    }

0 个答案:

没有答案