I need to modify an application that was custom built for us by someone else. The application accepts a file (of type .ydk) from a form and then uploads that to WordPress (while also reading it's contents). I want to modify this so that it can also read a .ydk file from the server.
I've narrowed it down to the following:
if($_FILES['deckFile']){
$attachment_ydk_id = upload_ydk_file($_FILES['deckFile']);
}
And the function it's then using:
//Upload ydk file
function upload_ydk_file( $file = array() ) {
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
$file_return = wp_handle_upload( $file, array('test_form' => false ) );
if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
if( 0 < intval( $attachment_id ) ) {
return $attachment_id;
}
}
return false;
}
So the way I look at it. $_Files
is needed since it's using wp_handle_upload
which I believe requires $_Files
array.
I tried fopen
and file()
as such but no luck:
$attachment_ydk_id = upload_ydk_file(fopen("location_of_file.ydk", "r"));
EDIT: 1st Attempt
I've now tried the following (in an attempt to re-create $_Files):
$urls = 'YGOPRO_Decks/user_decks/58535.ydk';
$size = filesize($urls);
$info = pathinfo($urls);
$info_basename = $info['basename'];
$info_mime = 'application/octet-stream';
$UrlArray = array(
'name' => $info_basename,
'type' => $info_mime,
'tmp_name' => 'YGOPRO_Decks/user_decks/58535.ydk',
'error' => 0,
'size' => $size
);
if($_FILES['deckFile']){
$attachment_ydk_id = upload_ydk_file($UrlArray);
}
The file exists in the location give. A print_r
on the array gives:
Array
(
[name] => 58535.ydk
[type] => application/octet-stream
[tmp_name] => YGOPRO_Decks/user_decks/58535.ydk
[error] => 0
[size] => 576
)
Still no luck unfortunately. This seemed like a really good method and I thought I was on to something but it continues to fail.
wp_handle_upload
calls_wp_handle_upload
, and the latter usesmove_uploaded_file
internally to move the file to its target location; but that PHP function has a build-in check to verify the file originated from an actual file upload, for security reasons - if you try to move any other file that doesn’t fulfill this requirement, it will fail.pre_move_uploaded_file
filter allows you to interfere at this point - you’d need to handle moving of the yourself in your custom handler function, and then returnnull
so that WordPress skips the part where it tries to move the file itself afterwards.$_FILES
? If the file is already on the server then just doing thewp_insert_attachment
stuff should be enough?null
to make WP skip the attempt to move the file. See line 848wp_handle_sideload
.