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

Php 如何在自定义插件数据库中插入Wordpress图像,php,wordpress,Php,Wordpress,我已经为data insert WP dashboard创建了一个插件。我正在尝试在我的数据库和插件文件夹中插入图像。数据已成功插入,但图像未插入。我是新开发人员 <?php function mywp_schools_create() { $code = $_POST["code"]; $name = $_POST["name"]; //insert if (isset($_POST['insert'])) { global $wpdb;

我已经为data insert WP dashboard创建了一个插件。我正在尝试在我的数据库和插件文件夹中插入图像。数据已成功插入,但图像未插入。我是新开发人员

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

试试这个代码

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

已更新

您没有为文件上载编写代码感谢级别,数据已插入。现在我想知道如何在我的服装图片文件夹中上传图片?你们在主题文件夹中上传吗?这是一个插件,我在wp内容->插件文件夹中上传。成功在数据库中提交数据,但未在wp content->uploads文件夹中上载图像。您的文档文件夹已自动创建,但未上载图像,数据传输成功。同样的问题更改此行$targetpath=上载主题路径。“/”$file\u名称;to$targetpath=上传主题路径。“/documents/”$file\u name;var_dump($code);显示:字符串'ABCD'(长度=4)变量转储($name);显示:字符串“HM学校”(长度=9)变量转储($tmp\U名称);秀:谢谢。它的工作和图像保存在上传文件夹中,但所有图像扩展名都是.tmp,如1482135513.tmp。我想要文件的默认扩展名更改此行$file\u name=pathinfo($tmp\u name,pathinfo\u FILENAME).time().“.”.pathinfo($tmp\u name,pathinfo\u扩展名);到$file\u name=pathinfo($tmp\u name,pathinfo\u FILENAME).time().“.”.pathinfo($\u FILES['photo']['name'],pathinfo\u扩展名);
    $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";
    }