doushang8512 2012-03-10 07:59
浏览 53
已采纳

PHP,JSON和JQuery上传

For the longest time, I've been using a very standard HTML post to my PHP upload script for my image upload. I wanted to try something new by sending the image to the PHP uplaod script via JQuery/JSON. All text data on my site is sent through this and I wanted to try and get image uploads to work with JQUery. I googled, but only found pre-made scripts, no one ever has a tutorial on making it yourself or where to begin, there's nothing wrong with using premade scripts but alot of times I can't get it to work with my website or style it so that it fits in, so I began to write a quick something, can you guys please tell me if this would work being that an image isn't text data?

My HTML:

<div class="upload_box_logged">
<input type="file" id="file_up"><br />
<button id="up_btn">Upload</button>
<em style="display:none; color:red;" id="no_file"  >Please Select a File!</em>
<em style="display:none; color:red;" id="up_fail" >Failed!</em>
<em style="display:none; color:red;" id="up_success" >Success!</em>
</div>

My JQuery:

$('#up_btn').click(function(){

var fileCheck = $('#file_up').val();
    if(fileCheck == '')
    {
        $('#no_file').fadeIn();
    }

    else
    {
        var file_upVar = encodeURIComponent($('#file_up').val());

             $.ajax({ 
            type: 'POST', url: 'upload.php',  dataType: "json",  data: { file_up: file_upVar  }, 
            success: function(result) {                 
                if (!result.success) { $('#up_fail').fadeIn().fadeOut(5000); }  
                else { $('#up_success').fadeIn().fadeOut(5000);  } 
            }  
        }); 


    }


});

Now here's my PHP file that I've been using with my standard HTML post to PHP method:

$file_name = $HTTP_POST_FILES['ufile1']['name'];
$file_name_for_db = ($HTTP_POST_FILES['ufile1']['name']);
$new_file_name = $id."_pic1_".$file_name;


    $allowedTypes = array("image/jpg", "image/jpeg", "image/png"); 
    $maxSize = 5 * 1024 * 1024; // 3Mb 

    $fileType = $HTTP_POST_FILES['ufile1']["type"]; 
    $fileSize = $HTTP_POST_FILES['ufile1']["size"]; 

    // check if there was an error 
    if ($HTTP_POST_FILES['ufile1']["error"] > 0) 
    { 
        die($HTTP_POST_FILES['ufile1']["error"]); 
    } 

    // check if the filetype is valid 
    if (!in_array($fileType, $allowedTypes)) 
    { 
        die("Invalid file type: $fileType"); 
    } 

    // check if the size doesn't exceed the limitations 
    if ($fileSize > $maxSize) 
    { 
        die("The file was too big: $fileSize"); 
    } 

    $name = $HTTP_POST_FILES['ufile1']["name"]; 
    $tmpfile = $HTTP_POST_FILES['ufile1']["tmp_name"]; 

    // check if the filename is valid 
    if (preg_match("/[\w-]+\.(jpg|jpeg|png)$/", $name) != 1) 
    { 
        die("Invalid file name: $name"); 
    } 

    $path = "../pic/";

    move_uploaded_file($tmpfile, $path); 

I will obviously have to de-code the post with something like this.. well I would if this is a correct way of going about it.

$uploaded_file = htmlspecialchars(trim(urldecode($_POST['file_up'])));

Well this is how I'd do it if it were text. Any way I can do this with image data?

Thanks a bunch -Mike

  • 写回答

3条回答 默认 最新

  • dongqiongjiong4740 2012-03-10 08:30
    关注

    Unfortunately I don't believe there is a way to post Images & Files via Ajax.

    The way I've normally done it in the past is creating a hidden iframe and making the form target the iframe name.

    So an example would look like:

    <form name="imageform" method='POST' action="upload_image.php" target="image_upload_frame" enctype="multipart/form-data">
        ...
    </form>
    <iframe name="image_upload_frame" height="1" width="1" style="visibility: hidden"></iframe>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据