dousha1873
2015-12-01 07:31
浏览 11

当我向代码添加ajax时图像不保存

I have a simple PHP file upload script like so ,

<?php

    $path = 'uploads/';
    $file_ext   = array('jpg','png','gif','bmp','JPG');
    $post_ext   = end(explode('.',$_FILES['photo']['name']));
    $photo_name = $_FILES['photo']['name'];
    $photo_type = $_FILES['photo']['type'];
    $photo_size = $_FILES['photo']['size'];
    $photo_tmp  = $_FILES['photo']['tmp_name'];
    $photo_error= $_FILES['photo']['error'];
    //move_uploaded_file($photo_tmp,"uploads/".$photo_name);

    echo $photo_tmp;

    if((($photo_type == 'image/jpeg') || ($photo_type == 'image/gif')   ||
       ($photo_type == 'image/png') || ($photo_type == 'image/pjpeg')) &&
       ($photo_size < 2000000) && in_array($post_ext,$file_ext)) {

        /* Understand in-Array !! */

        if($photo_error > 0 ){
            echo 'Error '.$photo_error;
            exit;
        }else{
            echo $photo_name.' Uploaded !';
        }
        if(file_exists($path.$photo_name)){
            echo 'There is '.$photo_name;
        }else{
            //new photo name and encryption
            $new_name = explode('.',$photo_name);
            $photo_name = 'erkan_'.md5($new_name[0]).'.'.$new_name[1];

            //move to directory
            if(move_uploaded_file($photo_tmp,$path.$photo_name)){

                return $photo_name;
            }
        }
    }

?>

The form code:

<form action="fileupload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="photo" id="fileBox">
            <button type="submit">SUBMIT</button>

Using the above script my file gets saved perfectly fine , but when i add a little ajax to the mix , like so:

$(function(){
    $('button[type="submit"]').on('click' , function(e){
        e.preventDefault();
        var formData = new FormData();
        formData.append('photo', $('input[type=file]')[0].files[0]); 
        $.ajax({
            url: 'fileupload.php',
            data: formData,
            // THIS MUST BE DONE FOR FILE UPLOADING
            contentType: false,
            processData: false,
            // ... Other options like success and etc
        });
    });
});

Now when i upload an image the image is not saved in my uploads folder , WHY ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doulieyu0881 2015-12-01 07:49
    已采纳

    You missed, type: "POST", in your <script></script> tag.

    $(function(){
        $('button[type="submit"]').on('click' , function(e){
            e.preventDefault();
            var formData = new FormData();
            formData.append('photo', $('input[type=file]')[0].files[0]); 
            $.ajax({
                type: "POST",  
                url: 'fileupload.php',
                data: formData,
                // THIS MUST BE DONE FOR FILE UPLOADING
                contentType: false,
                processData: false,
                // ... Other options like success and etc
            });
        });
    });
    
    点赞 打赏 评论
  • douluan5738 2015-12-01 07:35

    I think your file name is not correctly set. so your php is not able to get that file. Try this

     formData.append('photo', $('input[type=file]')[0].files[0],$('input[type=file]')[0].files[0].name); 
    
    点赞 打赏 评论
  • duanji7182 2015-12-01 17:37

    You have some errors in the ajax call.

    1- I gave your form an Id

    2- You are not getting the values from the form.

    $(function(){
        $('#submitBtn').on('click' , function(e){
            e.preventDefault();
                var form = $('form#myForm');
    
                var formdata = false;
    
                if (window.FormData){
                    formdata = new FormData(form[0]);
                }
    
                var formAction = form.attr('action');
    
                $.ajax({
                    url: formAction,
                    data : formdata ? formdata : form.serialize(),
                    cache : false,
                    contentType : false,
                    processData : false,
                    dataType: "json",
                    type : 'POST',
                    resetForm: true,
                })
                .done(function(data) {
                   //do something with the returned data
                }
         });
     });
    
    点赞 打赏 评论

相关推荐