0

I am writing a custom wordpress plugin that let the user upload a image to a folder. And I'm having problem saving the actual image to a folder.

This is my myplugin.php

add_action( 'admin_menu', 'register_my_custom_menu_page' );

function register_my_custom_menu_page(){
    add_menu_page( 'Home Page', 'Home Page', 'manage_options', 'homepage', 'my_custom_menu_page', plugins_url( 'myplugin/images/icon.png' ), 6 ); 
}

function my_custom_menu_page(){
    echo '<h1>Settings</h1>';
    echo '<form method="post" action="process.php" enctype="multipart/form-data">';
    echo '<table>';

    echo '<tr><td>Image 1</td><td><input type="file" name="file1" size="40"></td></tr>';    
    echo '<tr><td></td><td><input type="submit" name="submit" value="Update" /></td></tr>';     
    echo '</table>';
    echo '</form>';
 }

This is my process.php, and I do not see the image inside my wp upload folder. Please help!

move_uploaded_file($_FILES["cul_center"]["tmp_name"], wp_upload_dir().$_FILES["file1"]["name"]);
1
  • Whats cul_center key is? Commented Apr 28, 2014 at 18:41

1 Answer 1

1

You don't need to use PHP native functions for this.

Wordpress provide wp_handle_upload() function to handle uploads and move them /uploads/year/month/ explicitly.

Usage (in your case):

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

$uploadedfile = $_FILES['file1']; // your file input

$upload_overrides = array( 'test_form' => false ); // you need to do this according to docs

$movefile = wp_handle_upload( $uploadedfile, $upload_overrides ); //let it handle uploads

if ( $movefile ) {
    echo "File is valid, and was successfully uploaded.\n";
    var_dump( $movefile); // will print associative array of file attributes.
}
Sign up to request clarification or add additional context in comments.

Comments

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.