开发者

PHP upload issue with WordPress -> Want to upload to theme directory, but function is local to admin directory

开发者 https://www.devze.com 2023-02-12 05:36 出处:网络
I am trying to write a upload function in the WP functions.php. It works great, uploading the file to wp-admin, I want it to go into the themes directory - which I can access with get_bloginfo... but

I am trying to write a upload function in the WP functions.php. It works great, uploading the file to wp-admin, I want it to go into the themes directory - which I can access with get_bloginfo... but that is an absoluter address.

Is there a way to upload a file to a directory that is in the same site, but not the same directory?

The file structure is like this

wp-admin
wp-content
   themes
     Mytheme

the function runs in wp-admin, but I want it to go into my themes.

  $target_path = get_bloginfo('template_url');//NOT WORKING ABSOLUTE PATH

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
    echo(">> " . $target_path . " <<");
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile开发者_高级运维']['name']). 
    " has been uploaded";

} else{
    echo "There was an error uploading the file, please try again!";
}


Use get_bloginfo() with the stylesheet_directory parameters.

See: http://codex.wordpress.org/Function_Reference/get_bloginfo#Template_Directory for reference.

Try to modify your code like this:

 $target_path = get_bloginfo('stylesheet_directory');


just an addition: most of the time yous should store your uploaded files in the configured upload folder - get it with http://codex.wordpress.org/Function_Reference/wp_upload_dir

0

精彩评论

暂无评论...
验证码 换一张
取 消