0

我正在使用 php mysql 并使用 ajax(jquery) 上传图像。图像已成功上传,但没有立即显示,重新加载它正确显示的页面。

当我使用此代码时,上传和显示正在工作..

<?php
// Set the upload folder path
$target_path = "uploads/";
// Set the new path with the file name
$target_path = $target_path . basename( $_FILES['myfile']['name']); 

// Move the file to the upload folder
if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
// print the new image path in the page, and this will recevie the javascripts 'response' variable
echo $target_path;
} else{
// Set default the image path if any error in upload.
echo "default.jpg";
}

?>

但是,我不想使用正在上传的图像的原始名称将图像上传到上传目录,而是想将上传时的名称更改为 image01.jpg,如果存在具有此名称的图像,那么我希望它被替换为新的一个。为此,我使用此代码...

<?php
// Set the upload folder path
$place_file="";
$target_path = "1/";
$target_path1 = "1/image01.jpg";
// Set the new path with the file name
$target_path = $target_path ."image01.jpg"; 
// Move the file to the upload folder
$place_file=move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path1);
echo $target_path;
?>

图像上传正常,但没有立即显示,而是在页面刷新时显示。它仅显示以前的图像,我认为替换图像会导致问题。

我是文件上传编码的新手,这就是为什么有问题。

这个js代码上传图片

    new AjaxUpload(button,{
    action: 'upload.php', 
    name: 'myfile',
    onSubmit : function(file, ext){
        spinner.css('display', 'block');
        // you can disable upload button
        this.disable();
        },
    onComplete: function(file, response){
        button.stop(false,true).fadeOut(200);
        spinner.css('display', 'none');
        $('#profile_img').attr('src', response);
        // enable upload button
        this.enable();
    }
});

上面的php代码是upload.php

4

0 回答 0