支持上传文件的自定义插件不起作用

时间:2015-03-17 作者:gadss

我正在创建一个使用短代码的自定义插件。。然后,它将具有上载文件并将其存储在中的功能wp-content/uploads/ 目录但我似乎犯了个错误。。

Warning: move_uploaded_file(http://mysitecom/wp-content/uploads/myfiles/1991117- DriverLiscence.jpg): failed to open stream: HTTP wrapper does not support writeable connections in /home/user/public_html/mysite/wp-content/plugins/myplugin/functions.php on line 1266

Warning: move_uploaded_file(): Unable to move \'/tmp/phpOOkdNJ\' to \'http://mysitecom/wp-content/uploads/myfiles/1991117-DriverLiscence.jpg\' in /home/user/public_html/mysite/wp-content/plugins/myplugin/functions.php on line 1266
代码如下:

$upload_dir = wp_upload_dir(); 
$wp_upload_url =$upload_dir[\'baseurl\']; 
$uploaddir = $wp_upload_url."/myfiles/";

$temp = explode(".",$_FILES[$file_uploaded]["name"]);
$newfilename = $contactID. \'-\'. $type . \'.\' .end($temp);
$uploadfile = $uploaddir . $newfilename;
if (move_uploaded_file($_FILES[$file_uploaded][\'tmp_name\'], $uploadfile)) {
    echo \'success\';
}
纽约的问题是,我怎样才能得到/wp-content/uploads/myfiles/ 这样我就可以上传文件了?

2 个回复
SO网友:Rutunj sheladiya

使用Wordpress defult media uploader并在jquery响应中获取上载链接退出很容易

<label for="upload_image">
    <input id="upload_image" type="text" size="36" name="ad_image" value="http://" /> 
    <input id="upload_image_button" class="button" type="button" value="Upload Image" />
    <br />Enter a URL or upload an image
</label>

<?php
add_action(\'admin_enqueue_scripts\', \'my_admin_scripts\');

function my_admin_scripts() {
    if (isset($_GET[\'page\']) && $_GET[\'page\'] == \'my_plugin_page\') {
        wp_enqueue_media();
        wp_register_script(\'my-admin-js\', WP_PLUGIN_URL.\'/my-plugin/my-admin.js\', array(\'jquery\'));
        wp_enqueue_script(\'my-admin-js\');
    }
}

?>

<script>
    jQuery(document).ready(function($){


    var custom_uploader;


    $(\'#upload_image_button\').click(function(e) {

        e.preventDefault();

        //If the uploader object has already been created, reopen the dialog
        if (custom_uploader) {
            custom_uploader.open();
            return;
        }

        //Extend the wp.media object
        custom_uploader = wp.media.frames.file_frame = wp.media({
            title: \'Choose Image\',
            button: {
                text: \'Choose Image\'
            },
            multiple: true
        });

        //When a file is selected, grab the URL and set it as the text field\'s value
        custom_uploader.on(\'select\', function() {
            console.log(custom_uploader.state().get(\'selection\').toJSON());
            attachment = custom_uploader.state().get(\'selection\').first().toJSON();
            $(\'#upload_image\').val(attachment.url);
        });

        //Open the uploader dialog
        custom_uploader.open();

    });


});
    </script>

SO网友:grandcoder

您不能通过HTTP移动文件,而是需要使用本地路径移动文件。

(e.g. /home/content/site/folders/filename.php).
通常人们会将文件上传到媒体库。下面的代码显示了如何从插件上载文件。

if ($_FILES) {
    foreach ($_FILES as $file => $array) {
        if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
            return "upload error : " . $_FILES[$file][\'error\'];
        }
        $attach_id = media_handle_upload( $file, 0 );
    }   
}

结束