5

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.

5
  • 1
    wp_handle_upload calls _wp_handle_upload, and the latter uses move_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.
    – 04FS
    Commented Sep 30, 2019 at 11:15
  • 1
    But the 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 return null so that WordPress skips the part where it tries to move the file itself afterwards.
    – 04FS
    Commented Sep 30, 2019 at 11:16
  • 2
    Why do you need to inject data into $_FILES? If the file is already on the server then just doing the wp_insert_attachment stuff should be enough?
    – tobbr
    Commented Oct 2, 2019 at 11:30
  • @04FS You actually must return something different than null to make WP skip the attempt to move the file. See line 848 Commented Oct 3, 2019 at 10:12
  • 1
    Try to read about wp_handle_sideload.
    – yergo
    Commented Oct 3, 2019 at 16:25

3 Answers 3

2
+50

According to the documentation for wp_insert_attachement, the file must be located in the uploads directory. Assuming you have access to the full WordPress library (since you are already calling into it), you can get the path using wp_upload_dir.

$tmp_file = wp_upload_dir() . '/' . basename($urls);
if (!copy($url, $tmp_file)) 
    exit();

Note: If you don't have WordPress loaded (e.g. if you are trying to run this as a script from the command line) then the upload_ydk_file isn't going to work as it relies on several WordPress functions.

You also aren't actually ever calling upload_ydk_file because you are still checking if the deckFile node of the $_FILES array is set before calling it.

if($_FILES['deckFile']){
    $attachment_ydk_id = upload_ydk_file($UrlArray);
}

If you remove that and always call upload_ydk_file since you know that $UrlArray will always be set then you should be good to go.

$urls = 'YGOPRO_Decks/user_decks/58535.ydk';
$tmp_file = wp_upload_dir() . '/' . basename($urls);
if (!copy($url, $tmp_file)) 
    exit();

$size = filesize($tmp_file);
$info = pathinfo($tmp_file);
$info_basename = $info['basename'];
$info_mime = 'application/octet-stream';

$UrlArray = array(
    'name' => $info_basename,
    'type' => $info_mime,
    'tmp_name' => $tmp_file,
    'error' => 0,
    'size' => $size
);  

$attachment_ydk_id = upload_ydk_file($UrlArray);

EDIT: Using wp_handle_sideload is a safer way to do move the file into the uploads directory. However, it is going to move the file, not copy the file so if you want to use that instead you should still copy the original file before passing that to wp_handle_sideload.

2

You were on the right path by creating your own array for $_FILES. To continue on that, try adding the 'fake file' to the global like so:

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

$_FILES[$info_basename] = $UrlArray;

if(!empty($_FILES)) {
    $attachment_ydk_id = upload_ydk_file($UrlArray);
}

This should trick WP into thinking an actual file was uploaded.

0

Got around to finally implementing a solution for this. As a user kindly mentioned, wp_handle_sideload instead of wp_handle_upload was the solution. This allowed me to sideload a URL file alongside mimicing the $_FILES array.

//Upload ydk file
function upload_ydk_file( $file = array() ) {
    require_once( ABSPATH . 'wp-admin/includes/admin.php' );
      $file_return = wp_handle_sideload( $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;
}

wp_handle_sideload

Handle sideloads, which is the process of retrieving a media item from another server instead of a traditional media upload. This process involves sanitizing the filename, checking extensions for mime type, and moving the file to the appropriate directory within the uploads directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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