2012-03-10 102 views
4

最長的時間,我一直在使用一個非常標準的HTML文章來上傳我的PHP上傳腳本來上傳圖片。我想通過JQuery/JSON將圖像發送到PHP uplaod腳本來嘗試新的東西。我網站上的所有文本數據都是通過此發送的,我想嘗試獲取圖像上傳以與JQUery一起使用。我使用Google搜索,但只找到了預製腳本,沒有人有過自己製作或從哪裏開始的教程,使用預製腳本沒什麼問題,但很多時候我無法使用它來與我的網站或樣式一起工作它使它適合,所以我開始寫一個快速的東西,請你告訴我,如果這將工作,一個圖像不是文本數據?PHP,JSON和JQuery上傳

我的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> 

我的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); } 
      } 
     }); 


    } 


}); 

現在,這裏就是我一直在使用我的標準的HTML後到PHP的方法我的PHP文件:

$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); 

我顯然不得不用這樣的東西去解碼這個帖子..好吧,如果這是一個正確的方式去做它。

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

那麼這是我怎麼做,如果它是文本。任何方式,我可以做到這一點與圖像數據?

多謝 -Mike

+0

我就在想,你知道如何您可以將您的圖像文本在MySQL數據庫的加密的base64它存儲?我可以以某種方式將圖像分解爲jQuery中的base64,然後將其作爲文本發送到PHP文件,然後在那裏取消基礎64,重新渲染爲jpg或png,然後將其保存在我想要的位置...或者這太奇怪了嗎? – user1053263 2012-03-10 08:17:00

+0

我不確定是否有可能上傳這樣的文件。在表單上傳中,瀏覽器從本地磁盤讀取文件內容。在ajax中,沒有表單文章被執行,你將如何閱讀JavaScript內的本地文件的內容?它只能從文件輸入元素獲取文件名。我聽說你可以嘗試一種解決方案,即在包含表單的頁面中嵌入一個iframe來執行上傳,頁面中的腳本將操作iframe中的表單。所以上傳過程不會刷新頁面,而是iframe,它看起來像沒有頁面刷新的正常ajax動作。 – hago 2012-03-10 08:31:15

回答

3

不幸的是,我不相信有一種方法,通過Ajax張貼圖片&文件。

我過去通常的做法是創建一個隱藏的iframe,並使表單以iframe名稱爲目標。

因此,一個例子是這樣的:

<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> 
+0

這非常有意義!真棒邏輯!謝謝你,先生! – user1053263 2012-03-10 08:35:11