我正在从前端上传文件。它工作得很好,但我却发现了一个错误:
已定义常量ABSPATH
这是我的文件上载脚本的顶部:
require ($_SERVER[\'DOCUMENT_ROOT\'] . "/wp-load.php");
// require two files that are included in the wp-admin but not on the front end. These give you access to some special functions below.
require ($_SERVER[\'DOCUMENT_ROOT\'] . "/wp-admin/includes/file.php");
require ($_SERVER[\'DOCUMENT_ROOT\'] . "/wp-admin/includes/image.php");
wp-load.php
没有在其他任何地方手动加载,仅在此处加载。文件上传工作如期进行,唯一的问题是PHP正在发出通知。
如果我使用require_once
然后我得到更多的错误(尽管它仍然像预期的那样上传?)。
有什么想法吗?
Edit: more information
搜索/wp-content/*只会显示一次术语wp-load,这来自上述代码。
也许有一种方法可以自动加载wp负载。php,不包括短语wp load??
需要wp加载的代码位于主题模板(不是插件)中。特别是在允许从前端添加/编辑自定义帖子的页面模板中。
如果我移除需要wp负载的线路。php,则图像上传仍然有效。但是,我发现以下错误(目录结构中的敏感信息已被忽略):
Notice: Undefined offset: 1 in /<Path to file>/file_upload.php on line 25
Notice: Undefined offset: 1 in /<Path to file>/file_upload.php on line 26
Notice: Undefined offset: 1 in /<Path to file>/file_upload.php on line 27
Notice: Undefined offset: 1 in /<Path to file>/file_upload.php on line 28
Notice: Undefined offset: 1 in /<Path to file>/file_upload.php on line 29
Notice: Undefined offset: 2 in /<Path to file>/file_upload.php on line 25
Notice: Undefined offset: 2 in /<Path to file>/file_upload.php on line 26
Notice: Undefined offset: 2 in /<Path to file>/file_upload.php on line 27
Notice: Undefined offset: 2 in /<Path to file>/file_upload.php on line 28
Notice: Undefined offset: 2 in /<Path to file>/file_upload.php on line 29
Notice: Undefined offset: 3 in /<Path to file>/file_upload.php on line 25
Notice: Undefined offset: 3 in /<Path to file>/file_upload.php on line 26
Notice: Undefined offset: 3 in /<Path to file>/file_upload.php on line 27
Notice: Undefined offset: 3 in /<Path to file>/file_upload.php on line 28
Notice: Undefined offset: 3 in /<Path to file>/file_upload.php on line 29
Notice: Undefined offset: 4 in /<Path to file>/file_upload.php on line 25
Notice: Undefined offset: 4 in /<Path to file>/file_upload.php on line 26
Notice: Undefined offset: 4 in /<Path to file>/file_upload.php on line 27
Notice: Undefined offset: 4 in /<Path to file>/file_upload.php on line 28
Notice: Undefined offset: 4 in /<Path to file>/file_upload.php on line 29
Notice: Undefined offset: 5 in /<Path to file>/file_upload.php on line 25
Notice: Undefined offset: 5 in /<Path to file>/file_upload.php on line 26
Notice: Undefined offset: 5 in /<Path to file>/file_upload.php on line 27
Notice: Undefined offset: 5 in /<Path to file>/file_upload.php on line 28
Notice: Undefined offset: 5 in /<Path to file>/file_upload.php on line 29
此错误从此处引用$file\\u数组:
if ($count_files > 0) {
foreach ( range( 0, $count_files ) as $i ) {
// create an array of the $_FILES for each file
$file_array = array(
\'name\' => urlencode($_FILES[\'logo\'][\'name\'][$i]),
\'type\' => $_FILES[\'logo\'][\'type\'][$i],
\'tmp_name\' => $_FILES[\'logo\'][\'tmp_name\'][$i],
\'error\' => $_FILES[\'logo\'][\'error\'][$i],
\'size\' => $_FILES[\'logo\'][\'size\'][$i],
);
if ( !empty( $file_array[\'name\'] ) ) {
$uploaded_file = wp_handle_upload( $file_array, $upload_overrides );
$wp_filetype = wp_check_filetype( basename( $uploaded_file[\'file\'] ), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename( $uploaded_file[\'file\'] ) ),
\'post_content\' => \'logo\',
\'post_author\' => $logged_in_user,
\'post_status\' => \'inherit\',
\'post_type\' => \'attachment\',
\'post_parent\' => $_POST[\'post_id\'],
\'guid\' => $uploads[\'baseurl\'] . \'/\' . date( \'Y/m\' ) . \'/\' . $file_array[\'name\'],
\'post_date\' => date( \'Y-m-d H:i:s\' ),
\'post_date_gmt\' => date( \'Y-m-d H:i:s\' )
);
$attach_id = wp_insert_attachment( $attachment, \'/\' . date( \'Y/m\' ) . \'/\' . $file_array[\'name\'], $_POST[\'post_id\'] );
$attach_data = wp_generate_attachment_metadata( $attachment_id, $uploaded_file[\'file\'] );
wp_update_attachment_metadata( $attachment_id, $attach_data );
set_post_thumbnail( $_POST[\'post_id\'], $attach_id );
}
}
}