用逗号和其他特殊字符上传文件到wordpress

时间:2015-03-04 20:00:01

标签: php wordpress

我很难显示上传到Wordpress的特殊字符的文件。我的功能正常,直到我尝试上传像john,smith.jpg这样的文件。 Wordpress上传所有文件,所以这不是问题。问题是从Wordpress返回的url不会被解释为浏览器的有效源文件URL。

例如,当我上传文件marcia时,dave_03.jpg返回的网址为http://example.com/uploads/2015/03/Dove,Marcia_03.jpg

浏览器声称此资源不存在。我该怎么办? Janusz

if (in_array($_FILES['profile_photo']['type'], $types)) :
        // Your file handing script here
        $upload_overrides = array( 'test_form' => false );
        $movefile = wp_handle_upload( $_FILES['profile_photo'], $upload_overrides );
        if ( $movefile ) {
            //echo "File is valid, and was successfully uploaded.\n";

                //upload attachment
                $upload_dir = wp_upload_dir();
                //it must be absolute path to work
                $file =  $upload_dir['path'] ."/".$_FILES["profile_photo"]["name"];

                $guid = $upload_dir['url'] . '/' . basename( $_FILES["profile_photo"]["name"]);
                $attachment = array(
                    'guid'           =>$guid , 
                    'post_mime_type' => $_FILES["profile_photo"]["type"] ,
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $_FILES["profile_photo"]["name"]  ) ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                );
            // Insert the attachment.
            $photo_id = wp_insert_attachment( $attachment,$file);
            $src = wp_get_attachment_image_src( $photo_id); 

            $attach_data = wp_generate_attachment_metadata( $photo_id, $file);
            wp_update_attachment_metadata( $photo_id, $attach_data );

            User:: setUserPhoto($current_user_id, $photo_id);
        } else {
            echo "Possible file upload attack!\n";
    }   
    else:
    // Error, filetype not supported
        $messages[] = "This file type is not supported";
    endif;

0 个答案:

没有答案