I an creating a custom plugin that used shortcodes.. then it will have a feature that will upload files and stores it in wp-content/uploads/ directory.. but it seems I got an error..

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

this is the code:

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

ny question is that, how can I get the exact path of the /wp-content/uploads/myfiles/ so that I can upload files?

2 Answers 2


Use Wordpress defult media uploader and get uploaded link in jquery response it is quit easy

<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

add_action('admin_enqueue_scripts', 'my_admin_scripts');

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



    var custom_uploader;

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


        //If the uploader object has already been created, reopen the dialog
        if (custom_uploader) {

        //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() {
            attachment = custom_uploader.state().get('selection').first().toJSON();

        //Open the uploader dialog



You cannot move a file over HTTP, instead you need to move it using local path.

(e.g. /home/content/site/folders/filename.php).

normally people upload files to media library. The following code shows how to upload a file from a plugin.

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

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.