2017-06-09 40 views
1
上傳圖片

我試圖做的是從網址下載一些圖片,然後上傳到WordPress的帖子,我下面的形式:顯示響應狀態 - 使用JQuery

<form method="post" id="upload"> 
<?php foreach($data as $row): ?> 

<label>File Name</label> 
<input type="text" id="filename_<?php echo $row->id;?>" name="data[<?php echo $row->id;?>][filename]" value="<?php echo $file_name;?>" /> 

<label>Title</label> 
<input type="text" id="title_<?php echo $row->id;?>" name="data[<?php echo $row->id;?>][title]" value="<?php echo $title;?>" /> 

<?php endforeach;?> 
<button name="act" value="savepost">Save</button> 
</form> 
<!-- showing result here --> 
<div id="result"></div> 

所以,那麼當點擊保存按鈕......動作將調用功能prc_upload()

function prc_upload() { 
    $posts = $_POST['data']; 
    foreach ($posts as $post) { 
    $filename = $post['filename']; 
    $title  = $post['title']; 
    // Process uplaoding here 
    // Process uplaoding here 
    // Process uplaoding here 
    if (!$fileSaved) { 
     $result = $filename.' Can not saved !!'; 
    } else { 
     $result = filename' Saved Succesfuly!!'; 
    } 
} 

如何使用jQuery顯示響應結果時,保存按鈕點擊..

+0

'$ _POST'沒有文件數據。它是'$ _FILES'。 –

回答

0

追加你的IMG爲HTML

$($post).each(function(){ 
$('#placeIDwhereYouWantToShowImg').append('<img src='+this.imgsrc+' class="if any"/>') 
}) 
0

確保您的窗體有ENCTYPE = 「的multipart/form-data的」 屬性。

$new_post = wp_insert_post($post_array); 
if (!function_exists('wp_generate_attachment_metadata')){ 
       require_once(ABSPATH . "wp-admin" . '/includes/image.php'); 
       require_once(ABSPATH . "wp-admin" . '/includes/file.php'); 
       require_once(ABSPATH . "wp-admin" . '/includes/media.php'); 
      } 
      if ($_FILES) { 
       foreach ($_FILES as $file => $array) { 
        if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) { 
         return "upload error : " . $_FILES[$file]['error']; 
        } 
        $attach_id = media_handle_upload($file, $new_post); 
       } 
      } 
      if ($attach_id > 0){ 

       update_post_meta($new_post,'_thumbnail_id',$attach_id); 
      }