Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload File using a form in wordpress

Tags:

php

wordpress

I want to upload files to a folder called uploads in my wordpress theme. But it is not uploading the files. Can anybody please help , below is my code

HTML Code

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

[upload]

Below is php code of shortcode in functions.php file

functions upload ()
{
$file = $_FILES['file'];
$name = $file['name'];
$path = "/uploads/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
    echo " Move succeed";
} else {
 echo " Move failed. Possible duplicate?";
}
}

add_shortcode('upload','upload');

Thanks

like image 637
Zeeshan Avatar asked Apr 04 '15 10:04

Zeeshan


People also ask

Can you upload a file to Forms?

In Microsoft Forms, open the form you want to edit. Add new. Select More question types , and then select File upload. Note: File upload is only available when “Only people in my organization can respond” or “Specific people in my organization can respond” is the selected setting.

Can you upload documents to WordPress?

Upload PDF Files in WordPress To get started, login to your WordPress admin area and then go to Media. After that, click the “Add New” button. Simply drag and drop your PDF file in WordPress or click the “Select Files” button to upload it. After that, the PDF file will be uploaded to your WordPress website.


1 Answers

try to use this function .Define this in your function.php

<?php
function upload_user_file( $file = array() ) {

    require_once( ABSPATH . 'wp-admin/includes/admin.php' );

      $file_return = wp_handle_upload( $file, array('test_form' => false ) );

      if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
          return false;
      } else {

          $filename = $file_return['file'];

          $attachment = array(
              'post_mime_type' => $file_return['type'],
              'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
              'post_content' => '',
              'post_status' => 'inherit',
              'guid' => $file_return['url']
          );

          $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );

          require_once(ABSPATH . 'wp-admin/includes/image.php');
          $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
          wp_update_attachment_metadata( $attachment_id, $attachment_data );

          if( 0 < intval( $attachment_id ) ) {
            return $attachment_id;
          }
      }

      return false;
}
?>

and use like this

    <?php
    if(isset($_POST['upload']))
    {
       if( ! empty( $_FILES ) ) 
       {
          $file=$_FILES['file'];
          $attachment_id = upload_user_file( $file );

       }
    }
    ?>

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" name="upload">
</form>
like image 122
Manoj Dhiman Avatar answered Nov 02 '22 16:11

Manoj Dhiman