admin管理员组文章数量:1122826
The code I am using is practically the same as the codex. The $u and $m return true. Just the $error gives "specified file failed upload test". Thanks for any help you can give.
FORM:
PHP
if ($uploadedfile = $_FILES['amfile']) {$u = "Uploaded file set";} else {$u = "Uploaded file NOT set";}
$upload_overrides = array( 'test_form' => false );
if ($movefile = wp_handle_upload( $uploadedfile, $upload_overrides )) {$m = "Movefile file set";} else {$m = "Movefile file NOT set";} ;
if ( $movefile && !isset( $movefile['error'] ) ) {
$error = "File is valid, and was successfully uploaded.\n";
var_dump( $movefile );
} else {
$error = $movefile['error'];
}
echo $u . "<br>";
echo $m . "<br>";
echo $error . "<br>";
The code I am using is practically the same as the codex. The $u and $m return true. Just the $error gives "specified file failed upload test". Thanks for any help you can give.
FORM:
PHP
if ($uploadedfile = $_FILES['amfile']) {$u = "Uploaded file set";} else {$u = "Uploaded file NOT set";}
$upload_overrides = array( 'test_form' => false );
if ($movefile = wp_handle_upload( $uploadedfile, $upload_overrides )) {$m = "Movefile file set";} else {$m = "Movefile file NOT set";} ;
if ( $movefile && !isset( $movefile['error'] ) ) {
$error = "File is valid, and was successfully uploaded.\n";
var_dump( $movefile );
} else {
$error = $movefile['error'];
}
echo $u . "<br>";
echo $m . "<br>";
echo $error . "<br>";
Share
Improve this question
asked Jun 13, 2019 at 22:43
IanIan
11 silver badge2 bronze badges
2
- Which file you have to upload? – Milan Hirpara Commented Jun 14, 2019 at 6:31
- It's a pdf, doc or docx file. Loaded in a form and given like this: FORM: <input type="file" name="amfile" size="40" class="wpcf7-form-control wpcf7-file" id="amfile" accept=".pdf,.doc,.docx" aria-invalid="false"> – Ian Commented Jun 14, 2019 at 11:31
1 Answer
Reset to default 0Please add below code in your theme functions.php file :
add_filter( 'wp_check_filetype_and_ext', 'file_and_ext_allow_for_user', 10, 4 );
function file_and_ext_allow_for_user( $types, $file, $filename, $mimes )
{
if( false !== strpos( $filename, '.doc' ) ) {
$types['ext'] = 'doc';
$types['type'] = 'application/msword';
} else if( false !== strpos( $filename, '.pdf' ) ) {
$types['ext'] = 'pdf';
$types['type'] = 'application/pdf';
} else if( false !== strpos( $filename, '.docx' ) ) {
$types['ext'] = 'docx';
$types['type'] = 'application/docx';
}
return $types;
}
File Upload Code
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['amfile'];
if( $uploadedfile ){
$u = "Uploaded file set";
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && !isset( $movefile['error'] ) ) {
$m = "Movefile file set";
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
} else {
$m = "Movefile file NOT set";
/**
* Error generated by _wp_handle_upload()
* @see _wp_handle_upload() in wp-admin/includes/file.php
*/
print_r($movefile);
$error = $movefile['error'];
}
} else {
$u = "Uploaded file NOT set";
}
echo $u . "<br>";
echo $m . "<br>";
echo $error . "<br>";
本文标签: wphandleupload specified file failed upload test
版权声明:本文标题:wp_handle_upload specified file failed upload test 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290730a1928574.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论