如何在自定义插件数据库中插入Wordpress图像

时间:2016-12-18 11:46:09

标签: php wordpress

我已经为数据插入WP仪表板创建了一个插件。我想在我的数据库中插入图像&插件文件夹。数据已成功插入,但图像未插入。我是新开发者。

<?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
}

2 个答案:

答案 0 :(得分:0)

试试这段代码。

    $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";
    }

答案 1 :(得分:0)

    $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";
    }

<强>更新