Ajax图像上传和显示

时间:2012-10-29 09:36:13

标签: php ajax file-upload image-uploading

我正在使用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

0 个答案:

没有答案