前端文件上载工作正常,但我收到错误消息“常量ABSPATH已定义”

时间:2013-10-14 作者:Joe Tannorella

我正在从前端上传文件。它工作得很好,但我却发现了一个错误:

已定义常量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 );

        }
    }
}

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

需要wp加载的代码位于主题模板(不是插件)中。特别是在允许从前端添加/编辑自定义帖子的页面模板中。

页面模板不需要显式加载WordPress核心。如果尚未加载WordPress,则不会加载模板本身。

你的其他错误是因为$_FILES[\'logo\'][\'type\'][1], $_FILES[\'logo\'][\'type\'][2], $_FILES[\'logo\'][\'type\'][3], 或$_FILES[\'logo\'][\'type\'][4]. 你需要var_dump($_FILES); 并确切了解您正在处理的内容,但这是一个纯粹的PHP问题,可能会偏离主题。

结束

相关推荐

上传的图像不在/Uploads文件夹中

网站存在巨大问题。我更新了主题以使用帖子缩略图(这是多么旧),然后在尝试上载图像以用作特色缩略图时:图像会处理/压缩,但URL会导致404。网站没有任何异常。htaccess。实际上没有wp content/uploads目录,所以我做了一个,chmod权限是0755,所以这不应该是问题所在。我将“媒体设置”文件夹重置为默认文件夹,但该文件夹中不会出现任何图像。编辑:将上载文件夹更改为0777权限似乎可以解决此问题。这样保留权限安全吗?