2016-09-16 48 views
0

我確信我在這個上犯了一個簡單的錯誤。該文件沒有被傳遞到PHP頁面(我得到了「沒有上傳:文件錯誤,請再試一次」錯誤傳回來的PHP頁面。)單個文件上傳jquery php

沒有JQUERY的代碼工作正常,所以我猜PHP不是問題。

我檢查了字段名稱/編號&它似乎確定。我試着測試一下按鈕的值是否被通過,結果不是這樣,當我得出結論時,它可能是JQUERY,但後來我被困住了。

我知道類似的帖子已提交(我讀過)但我絕望!

預先感謝您。

這裏的HTML:

<form action="upload.php" name="formname" ENCTYPE="multipart/form-data" id="formid"> 
     <fieldset> 
      <legend>File info</legend> 
      <p> 
       <label for="file1">Files<span class="red"> *</span></label> 
       <input name="file1" type="file" /> 
      </p> 
     </fieldset> 
     <p><label>&nbsp;</label><input type="button" id="submit" value="Add" /></p> 
    </form> 
    <div id="output"></div> 

<script> 
$(function(){ 
    $('#submit').on('click', function(){ 
     var fd = new FormData($("#formid")); 
     $.ajax({ 
      url: 'upload.php', 
      type: 'POST', 
      data: fd, 
      success:function(data){ 
       $('#output').html(data); 
      }, 
      cache: false, 
      contentType: false, 
      processData: false 
     }); 
    }); 
}); 
</script> 

和PHP

<?php 
    $strName = 'JoeBloggs'; 
    $intId = 1045; 
    $missing = ''; 
    date_default_timezone_set("Europe/London"); 
    error_reporting(E_ALL); 
    include('class.upload.php'); 
    // where to put the images? 
    $dir_dest = '../'.$intId.'/'; 
    $xcount = 0; 
    if (!file_exists($dir_dest)) { mkdir($dir_dest, 0777, true);} 
    # upload 1400px 
    $handle = new upload($_FILES['file1']); 
    if ($handle->uploaded) { 
     $y = $handle->image_src_y; 
     $height = $y/2; 
     $handle->image_resize   = true; 
     $handle->image_ratio_crop  = true; 
     $handle->image_x    = 700; 
     $handle->image_y    = 260; 
     $handle->image_convert   = jpg; 
     $handle->Process($dir_dest); 
     if ($handle->processed) { 
      // everything was fine ! 
      $strFilename1 = $handle->file_dst_name; 
      rename($dir_dest . $strFilename1, $dir_dest . $strName . '.jpg'); 
      $missing .= 'File uploaded'; 
     } else { 
      $missing .= $missing. 'Not processed:' . $handle->error . '<br />'; 
     } 
    } else { 
     $missing .= 'Not uploaded:' . $handle->error . '<br />'; 
    } 

    echo $missing; 
    //header('Location: ../index.php?missing='.$missing); 
?> 
<img src="<?= $dir_dest . $strName?>.jpg" /> 
+0

[jQuery AJAX文件上傳PHP]可能的重複(http://stackoverflow.com/questions/23980733/jquery-ajax-file-upload-php) –

回答

1

嘗試,這將可以幫助你,

<script> 
$(function(){ 
    $('#submit').on('click', function(){ 
    var form = $('#formid')[0]; 
    var formData = new FormData(form); 
    $.ajax({ 
     url: 'upload.php', 
     type: 'POST', 
     data: formData, 
     success:function(data){ 
      $('#output').html(data); 
     }, 
     cache: false, 
     contentType: false, 
     processData: false 
    }); 
}); 
}); 
</script> 
+0

感謝您花時間看我的代碼 - 成功! – GeneralTomfoolery

+0

很高興幫助你..! – kc1994

0

嘗試加入到AJAX功能:

contentType: multipart/form-data