<form enctype="multipart/form-data" method="post" action="uploader.php">
<input type="file" name="pic" /><br />
<input type="submit" value="Upload File" />
</form>
$file_title = $_FILES["pic"]["name"];
echo "$file_title";
in wordpress functions.php file; custom field methos are:
function credits_meta() {
global $post;
$custom = get_post_custom($post->ID);
$designers = $custom["designers"][0];
$developers = $custom["developers"][0];
$producers = $custom["producers"][0];
?>
<form method="POST" enctype="multipart/form-data">
<p><label>Designed By:</label><br />
<textarea cols="50" rows="5" name="designers"><?php echo $designers; ?></textarea></p>
<p><label>Built By:</label><br />
<textarea cols="50" rows="5" name="developers"><?php echo $developers; ?> </textarea></p>
<p><label>Upload Image :</label><br />
<input type="file" name="myPhoto" size="25"/></p>
</form>
<?php
}
function save_details(){
global $post;
$target_path = get_bloginfo('template_directory')."/images/";
$file_title = $_FILES["myPhoto"]["name"];
$new_file_title = "wp_".$file_title;
update_post_meta($post->ID, "year_completed", $_POST["year_completed"]);
update_post_meta($post->ID, "designers", $_POST["designers"]);
update_post_meta($post->ID, "developers", $_POST["developers"]);
update_post_meta($post->ID, "producers", $new_file_title);
}
when i try the above code with core php it works fine but, when i try to do the same in wordpress custom fields image uploading: $_FILES
alwasy gives empty.
it gives the name of the image if i use $_POST["pic"];
I have tried to check this with print_r
, var_dumb and even in wordpress functions.php file with this:
add_action('init', 'myfunction');
function myfunction(){
if($_FILES){
die("something");
}
}
still it gives empty. the file size which i'm trying to upload is 153kb.
My php.ini file:
file_uploads = on;
upload_max_filesize = 2M
Any help would be appreciated
Adding the following hook in the functions.php file solves the problem.
add_action( 'post_edit_form_tag' , 'post_edit_form_tag' );
function post_edit_form_tag( ) {
echo ' enctype="multipart/form-data"';
}
remove action
<form enctype="multipart/form-data" method="post" action="">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With