Reputation: 7986
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"]);
Upvotes: 0
Views: 2948
Reputation: 10132
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.
}
Upvotes: 1