【问题标题】:Save file uploaded by user to wordpress uploads folder将用户上传的文件保存到 wordpress 上传文件夹
【发布时间】:2013-06-07 10:55:01
【问题描述】:

我在 wordpress 中创建了一个页面,并在其中放置了此表单。

<form action=""method="post"enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="text" placeholder="Your Name" name="name" />
<input type="submit" name="submit" value="Submit">
</form>

现在点击提交时,我希望将用户上传的图片保存在文件夹 wp-content/uploads/[发布的用户名]

谁能帮我处理wordpress中的后端处理部分。我试过wp_upload_bits,但不能用它。

提前致谢。

【问题讨论】:

  • 请添加您编写的代码作为起点 - 而不是期望有人为您提供正确的代码......付出一些努力,其他人可能会做出类似的回应

标签: php wordpress


【解决方案1】:

用户在wordpress的insert_attachment保存附件,那么你也将拥有获取不同风格图片的功能,get_attachment(id, 'full/medium/thumbnail')。

$files = $_FILES['upload_attachment'];
foreach ($_FILES as $file => $array) {
  $newupload = insert_attachment($file,null);
  // save the $newupload with you from data, as psot id for the attachment
 }
 function  insert_attachment($file_handler,$post_id,$setthumb='false') {

    // check to make sure its a successful upload
    if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

      require_once(ABSPATH . "wp-admin" . '/includes/image.php');
      require_once(ABSPATH . "wp-admin" . '/includes/file.php');
      require_once(ABSPATH . "wp-admin" . '/includes/media.php');
      $attach_id = media_handle_upload( $file_handler, $post_id );

   if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
     return $attach_id;
 } 

在 html 中,如果您提交表单,将上传图片并将引用保存在 wp_post 表中

<form action="upload_file.php" method="post" enctype="multipart/form-data">
   <label for="file">Filename:</label>
   <input type="file" name="upload_attachment[]" id="file"><br>
   <label for="file">Filename:</label>
   <input type="file" name="upload_attachment[]" id="file"><br>
   <label for="file">Filename:</label>
   <input type="file" name="upload_attachment[]" id="file"><br>
   <input type="submit" name="submit" value="Submit">
</form>

【讨论】:

  • 我不想用上传的图片更新帖子。我只想将用户上传的图片保存到指定文件夹
  • 会上传到wordpress的上传文件夹中,i
  • 我希望它上传到文件夹 wp-content/uploads/[发布的用户名]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 2019-03-17
  • 2015-04-28
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多