简体   繁体   中英

How to Wordpress image insert in custom plugin database

I have created a plugin for data insert WP dashboard. I am trying to insert images in my database & plugin folder. Data has inserted successfully, but the image doesn't insert. I am new developer.

<?php

function mywp_schools_create() {
    $code = $_POST["code"];
    $name = $_POST["name"];
    //insert
    if (isset($_POST['insert'])) {
        global $wpdb;
        $table_name = $wpdb->prefix . "school";
        $wpdb->insert(
                //'school', //table
                $table_name, //table
                array('code' => $code, 'name' => $name) //data
                //array('%s', '%s') //data format           
        );
        $message.="Data inserted";
    }
    ?>
    <link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>mywp/sinetiks-schools/style-admin.css" rel="stylesheet" />
    <div class="wrap">
        <h2>DATA</h2>
        <?php if (isset($message)): ?><div class="updated"><p><?php echo $message; ?></p></div><?php endif; ?>
        <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
            <table class='wp-list-table widefat fixed'>
                <tr>
                    <th class="ss-th-width">Code</th>
                    <td><input type="text" name="code" value="<?php echo $id; ?>" class="ss-field-width" /></td>
                </tr>
                <tr>
                    <th class="ss-th-width">School</th>
                    <td><input type="text" name="name" value="<?php echo $name; ?>" class="ss-field-width" /></td>
                </tr>
                 <tr>
                    <th class="ss-th-width">Image</th>
                    <td><input type="file" name="photo" value="" class="ss-field-width" /></td>
                </tr>
            </table>
            <input type='submit' name="insert" value='Save' class='button'>

        </form>
    </div>
    <?php
}

Try this code.

    $code = $_POST["code"];
    $name = $_POST["name"];
    $tmp_name = $_FILES["photo"]["tmp_name"];



    //insert
    if (isset($_POST['insert'])) {
    global $wpdb;
    $table_name = $wpdb->prefix . "school";
    $path_array = wp_upload_dir(); // normal format start
    $file_name   =   pathinfo($tmp_name ,PATHINFO_FILENAME).time().".".pathinfo($tmp_name ,PATHINFO_EXTENSION);  
    $imgtype     =   strtolower(pathinfo($tmp_name,PATHINFO_EXTENSION));                
    $targetpath        =   $path_array["path"]."/".$file_name;

    move_uploaded_file($tmp_name, $targetpath );

    $wpdb->insert(
        //'school', //table
        $table_name, //table
        array('code' => $code, 'name' => $name,'image_name'=>$file_name) //data
        //array('%s', '%s') //data format           
    );
    $message.="Data inserted";
    }
    $code = $_POST["code"];
    $name = $_POST["name"];
    $tmp_name = $_FILES["photo"]["tmp_name"];



    if (!file_exists(dirname(__FILE__))) {
        mkdir('documents', 0777, true);
    }

    $FolderUrl   = dirname(__FILE__).'/documents/';

    if (!file_exists($FolderUrl)) {
        mkdir($FolderUrl, 0777, true);
    }


    define('UPLOADS_THEME_PATH',$FolderUrl);

    //insert
    if (isset($_POST['insert'])) {
        global $wpdb;
        $table_name = $wpdb->prefix . "school";
        $path_array = wp_upload_dir(); // normal format start
        $file_name   =   pathinfo($tmp_name ,PATHINFO_FILENAME).time().".".pathinfo($_FILES['photo']['name'] ,PATHINFO_EXTENSION); 
        $imgtype     =   strtolower(pathinfo($tmp_name,PATHINFO_EXTENSION));                
        $targetpath  =   UPLOADS_THEME_PATH."/documents/".$file_name;

        move_uploaded_file($tmp_name, $targetpath );

        $wpdb->insert(
            //'school', //table
            $table_name, //table
            array('code' => $code, 'name' => $name,'image_name'=>$file_name) //data
            //array('%s', '%s') //data format           
        );
        $message.="Data inserted";
    }

Updated

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM