如果要在自定义文件夹中上载,可以使用以下功能。在函数中添加函数。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\']);
}
}
}
在模板文件中创建如下表单:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="upload">
</form>
像以前一样在模板文件中调用upload函数
get_header()
功能:
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);
}
}