doushui5587 2015-06-27 06:02
浏览 38
已采纳

图片上传到mysql数据库无法正常工作

I am loading an image file using jquery filedialog. And then sending an ajax request to a php file to upload the image to database. Here is my ajax request being sent

$("#imgfile").on("change",function(e){
                    var file_data = $('#imgfile').prop('files')[0]; 
                e.preventDefault();
                var form_data = new FormData();                  
                form_data.append('file', file_data);
                //alert(form_data); 
                $.ajax({
            url: 'changeProfileImg.php', // point to server-side PHP script 
            contentType: "image/jpeg",  // what to expect back from the PHP script, if anything
            cache: false,
            dataType: "image",
            processData: false,
            data: form_data,                         
            type: 'post',
            success: function(php_script_response){
                alert(php_script_response); // display response from the PHP script, if any
            }
            }); 
                });

Here is my php code

include_once 'dbconnect.php';
    if ( 0 < $_FILES['file']['error'] ) {
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else {
        //move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
        $file=$_FILES['file']['name'];
        $id=$_SESSION['id'];
        $sql="UPDATE  mainadmin set photo='$file' where id='$id'";
        mysql_query($sql);
        echo 'success';
        $_SESSION['photo']=$file;
    }

But i see some garbase thing is being inserted into database not showing the image. How can i do it working.

  • 写回答

1条回答 默认 最新

  • douaikuai2715 2015-06-27 07:16
    关注

    See below URL and code I think this is helpful to you.

    Simple jQuery progress bar percentage

    This is my tested code file upload with progress bar percentage

    But u can manage or modify as per Your requirement.

    <!doctype html>
    <head>
    <title>File Upload Progress Demo #1</title>
    <style>
    body { padding: 30px }
    form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
    
    .progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
    .bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
    .percent { position:absolute; display:inline-block; top:3px; left:48%; }
    </style>
    </head>
    <body>
        <h1>File Upload Progress Demo #1</h1>
        <code>&lt;input type="file" name="myfile"></code>
            <form action="upload.php" method="post" enctype="multipart/form-data">
            <input type="file" name="uploadedfile"><br>
            <input type="submit" value="Upload File to Server">
        </form>
    
        <div class="progress">
            <div class="bar"></div >
            <div class="percent">0%</div >
        </div>
    
        <div id="status"></div>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
    <script src="http://malsup.github.com/jquery.form.js"></script>
    <script>
    (function() {
    
    var bar = $('.bar');
    var percent = $('.percent');
    var status = $('#status');
    
    $('form').ajaxForm({
        beforeSend: function() {
            status.empty();
            var percentVal = '0%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        uploadProgress: function(event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal)
            percent.html(percentVal);
        },
        complete: function(xhr) {
         bar.width("100%");
        percent.html("100%");
            status.html(xhr.responseText);
        }
    }); 
    
    })();       
    </script>
    
    </body>
    </html>
    

    my php code

    <?php
    $target_path = "uploads/";
    
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line