Frontend file upload works, but I get error Constant ABSPATH already defined
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 );
}
}
}