Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am uploading files from the frontend. It works perfectly, yet I am getting the error:

Constant ABSPATH already defined

This is at the top of my file upload script:

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 is not being loaded anywhere else manually, only here. The file upload works as expected, the only issue is the Notice that PHP is spitting out.

If I use require_once then I get more errors (though it does still upload as expected?).

Any ideas?

Edit: more information

A search through /wp-content/* shows the term wp-load only once - that is from the above code.

Maybe there is a way to autoload wp-load.php without including the phrase wp-load??

The code requiring wp-load is within a theme template (not a plugin). Specifically, within a page-template that allows a custom post to be added/edited from the frontend.

If I remove the line that requires wp-load.php, then the image upload still works. However, I get the following errors (sensitive info in directory structure has been omitted):

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

This error is referencing the $file_array array from here:

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 );

        }
    }
}
share|improve this question
    
Then the file is being loaded automatically for some reason. Please add more detail to this question, and for the record, explicitly requireing files is nearly always "doing it wrong". –  s_ha_dum Oct 14 '13 at 14:45
    
Thanks for responding, @s_ha_dum. I have added some more information to the question - if there is anything else I should add please let me know. What would the correct alternative to require-ing be? –  Joe T Oct 14 '13 at 16:35
    
"What would the correct alternative to require-ing be?" -- the AJAX API solves a lot of these kinds of issues, and despite the name the request doesn't actually have to be initiated by Javascript. A request is a request. –  s_ha_dum Oct 14 '13 at 20:41

1 Answer 1

up vote 0 down vote accepted

The code requiring wp-load is within a theme template (not a plugin). Specifically, within a page-template that allows a custom post to be added/edited from the frontend.

A page template should not require any explicit loading of the WordPress Core. The template itself wouldn't load if WordPress were not already loaded.

Your other errors are because there are apparently no $_FILES['logo']['type'][1], $_FILES['logo']['type'][2], $_FILES['logo']['type'][3], or $_FILES['logo']['type'][4]. You need to var_dump($_FILES); and see exactly what you are dealing with, but that is a pure PHP problem and would off-topic here.

share|improve this answer
    
Perfect. After some digging around I changed the loop and now it's working fine. Thanks a lot for your time and effort! –  Joe T Oct 15 '13 at 14:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.