0

I have some code that uploads a file. I've tried both wp_handle_upload as well as regular PHP code and with both, it will claim it uploaded the files (wp_handle_upload returns an array with "url" and "file" filled) but the files are not in the folder!

NOTE: This is called via an "wp_ajax_nopriv_" function

$uploadedFile = wp_handle_upload( $_FILE[ "thefile" ], array( "test_form" => false ) );

//It makes it past this
if ( !$this->isUploaded( $uploadedFile ) ) return false;

$filePath = $uploadedFile[ "file" ];
$fileType = wp_check_filetype( basename( $filePath ) );

$attachmentInfo = array(
    "post_mime_type" => $fileType[ "type" ],
    "post_title" => $title,
    "post_status" => "inherit",
    "post_content" => ""
);

$attachmentId = wp_insert_attachment( $attachmentInfo, $filePath, $postId );
if ( $attachmentId === 0 ) return false;
$attachmentData = wp_generate_attachment_metadata( $attachmentId, $filePath );
if ( wp_update_attachment_metadata( $attachmentId, $attachmentData ) === false ) return false;

return true;

Pure PHP uploading works, it's only through wordpress that it won't upload!

Here's the code that works (but only if called outside WordPress):

if ( $_SERVER[ "REQUEST_METHOD" ] === "POST" )
{
    foreach ( $_FILES as $file )
    {
        //This is never called
        if ( $file[ "error" ] !== 0 ) throw new Exception( "Error: " . $file[ "name" ] );

    $index = 0;
    while ( file_exists( "/var/www/wp-content/uploads/" . $index . $file[ "name" ] ) ) $index++;
    $name = "/var/www/wp-content/uploads/" . $index . $file[ "name" ];
    move_uploaded_file( $file[ "tmp_name" ], $name );
    }
}
  • Please explain how this question is different from this one? – s_ha_dum Apr 29 '14 at 1:35
  • Whoops! sorry, didn't realize I'd posted on this forum already. I haven't had any answers or even help on any forum so I've been trying others. Since this one has more info, I'll delete the other. – Don Rhummy Apr 29 '14 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.