Reputation: 2104
I have created a page in wordpress and in that i have put this form.
<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>
Now on clicking submit i want the pic uploaded by user to be saved in folder wp-content/uploads/[posted user name]
Can anyone please help me with the backend processing part in wordpress.I have tried wp_upload_bits,but could nt go with it.
Thanks in advance.
Upvotes: 1
Views: 6563
Reputation: 1556
user insert_attachment of wordpress to save attachment, then you will also have the functionality to get the images with different styles, 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;
}
in html , if you submit the form, images will be uploaded and referances will be saved in wp_post table
<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>
Upvotes: 5