2017-06-09 44 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); 
      }