2016-05-16 2 views
2

私はカスタムページテンプレートを持っています。このテンプレートには、ユーザーがファイルをアップロードできるカスタムフォームがあります。これで、アップロードされたファイルはMySQLではなくファイルシステム/ローカルフォルダ(ローカルコンピュータ)にアップロードされます。WordPressのカスタムページテンプレートでファイルをアップロードするには?

私はすでにPHPコードを持っていますが、コードが動作していません。エラーが発生しています。

これを修正するにはどうすればよいですか?

スニペット:

if(isset($_POST['submit'])){ 
    $lastName = isset($_POST['lastName']) ? $_POST['lastName'] : ''; 
    $resumeFile = isset($_FILE['resumeFile']['name']) ? $_FILE['resumeFile']['name']: ''; 

$info = pathinfo($_FILES['resumeFile']['name']); 
$ext = $info['extension']; 
$file_rename= $lastName.$ext; 

$target = 'C://xampp/htdocs/files/'.$file_rename; 
move_uploaded_file($_FILES['resumeFile']['tmp_name'], $target); 
} 

エラー:

Notice: Undefined index: resumeFile .... on line 130 
Notice: Undefined index: extension .... on line 131 
Notice: Undefined index: resumeFile .... on line 135 

答えて

1

がデフォルトuploadsディレクトリ以外の別のディレクトリにファイルをアップロードするには、次のスニペットを試してみてください。

function change_my_upload_directory($dir) { 
    return array (
     'path' => WP_CONTENT_DIR . '/your-custom-dir', 
     'url' => $dir['baseurl'] . '/wp-content/your-custom-dir', 
     'subdir' => '/your-custom-dir', 
    ) + $dir; 
} 

if(isset($_FILES[ "resumeFile" ])) { 

    if (!function_exists('wp_handle_upload')) { 
     require_once(ABSPATH . 'wp-admin/includes/file.php'); 
    } 
    // Overrides the uploads directory location 
    add_filter('upload_dir', 'change_my_upload_directory');  

    $movefile = wp_handle_upload($_FILES[ "resumeFile" ], array('test_form' => false)); 

    // Remove the filter, so that subsequant uploads will be dsent to default uploads directory 
    remove_filter('upload_dir', 'change_my_upload_directory'); 

    return $movefile; 

}  
return false; 

wp_handle_uploadwp_handle_uploadが、それはエラーオブジェクトを返します。失敗した場合、あなたは何であるか、この

if(isset($movefile[ 'error' ])) { 
    // handle the error 
} 
+0

のような失敗したアップロードを確認することができ、以下の特性に

$movefile[ 'file' ] // uploaded file object $movefile[ 'url' ] // current url (file location) of the file $movefile[ 'type' ] // uploaded file type 

を含むオブジェクトを返します。ファイルはに保存されますか?ファイルシステム/ローカルコンピュータにファイルを保存する必要があります – User014019

+0

ファイルは '/ wp-content/uploads /'ディレクトリにアップロードされます。はい、あなたのローカルPCになります(ワードプレスがローカルマシンにある場合)。サーバー(クラウド上のホストされたマシン) – Sark

+0

'test_form'は私のhtmlフォームの名前ですよね? – User014019

関連する問題