duanbei7035 2014-04-13 18:28
浏览 45
已采纳

将新文件名传递回jquery

I am using some jquery to help upload a file to a php script. Everything is working fine and the file does in fact get uploaded. But during the upload, I have made it so the file gets resized to our needs, with a new unique file name. I'd like to pass that new unique file name back to the jquery and have it display on the page. Right now, it just displays the original image (which is not resized)

Here's the jquery code:

    $(function(){
    var btnUpload=$('#upload');
    var status=$('#status');
    new AjaxUpload(btnUpload, {
        action: 'upload-file.php',
        name: 'uploadfile',
        onSubmit: function(file, ext){
             if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                // extension is not allowed 
                status.text('Only JPG, PNG or GIF files are allowed');
                return false;
            }
            status.text('Uploading...');
        },
        onComplete: function(file, response){
            //On completion clear the status
            status.text('');
            //Add uploaded file to list
            if(response==="success"){
                $('<li></li>').appendTo('#files').html('<img src="./uploads/'+file+'" alt="" /><br />'+file).addClass('success');
            } else{
                $('<li></li>').appendTo('#files').text(file).addClass('error');
            }
        }
    });

});

And then my upload php file looks like this:

                $uploaddir = 'uploads'; 
            $file = $uploaddir . basename($_FILES['uploadfile']['name']); 

            if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 

                    $path = realpath(dirname(__FILE__));
                    include $path . '/uploads/phmagick.php'; 


                    $temp_file = explode(".", $_FILES['uploadfile']['name']);
                    $time = time();

                    $new_file = $time . '.' . $temp_file[1];



                    $p = new phmagick($path . '/uploads/' . $_FILES['uploadfile']['name'], $path . '/uploads/' . $new_file); 

                    $p->convert();


                    $phMagick = new phMagick($path . '/uploads/' . $new_file, $path . '/uploads/' . $new_file); 
                    $phMagick->debug=true; 
                    $phMagick->resize(414,414,true); 


              echo "success"; 
            } else {
                echo "error";
            }

Any thoughts on how I can get the new unique file name back, which would be something like: 1397413326.jpg?

Thank you

  • 写回答

1条回答 默认 最新

  • douyi1939 2014-04-13 18:33
    关注

    Echo the filename back instead of the word "success".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化