6

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

2 Answers 2

5

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>
Sign up to request clarification or add additional context in comments.

4 Comments

thanks but how will this function run in functions.php file ... we have not used any function call or can I use a shortcode please guide and also kindly note that there is a custom folder not the wordpress folder where I want to upload file ... thanks
why you want to use shortcode ?? and you will get attachment id you can get image through attachment id of any size you want . and we call this function on the page where the form exists.
Infact I am creating an image upload competition functionality where users can upload images and others can rate and their will be a winner who has the most votes. So how can I upload to a custom folder other then the default upload folder
There is a tiny error in the script: $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] ); should be: $attachment_id = wp_insert_attachment( $attachment, $file_return['file'] );
3

if you want to upload in a custom folder you can use this function

add the function in functions.php

function upload_user_file( $file = array(),$path ) {
    if(!empty($file)) 
    {


        $upload_dir=$path;
        $uploaded=move_uploaded_file($file['tmp_name'], $upload_dir.$file['name']);
        if($uploaded) 
        {
            echo "uploaded successfully ";

        }else
        {
            echo "some error in upload " ;print_r($file['error']);  
        }
    }

}

make a form in your template file like this

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

call the upload function in template file like before get_header(); function

if(isset($_POST['upload']))
    {

       if( ! empty( $_FILES ) ) 
       {
          $file=$_FILES['file'];   // file array
          $upload_dir=wp_upload_dir();
          $path=$upload_dir['basedir'].'/myuploads/';  //upload dir.
          if(!is_dir($path)) { mkdir($path); }
          $attachment_id = upload_user_file( $file ,$path);

       }
    }

2 Comments

i know this old, but how would you echo the image path after uploading?
5 years later. I'm still waiting for an answer for the question @730wavy brought up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.