Is it possible to mimic or pass a file url to $_Files?

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:

    $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

    $attachment_ydk_id = upload_ydk_file($UrlArray);

The file exists in the location give. A print_r on the array gives:

    [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.

Read more here:

Content Attribution

This content was originally published by GenesisBits at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: