Wordpress自定义表单与fileupload

时间:2017-03-10 00:41:22

标签: php wordpress image upload

我有一个自定义wordpress php页面,其中我有一个表单,用发布的数据输入字段更新数据库。 现在,我无法上传特定文件夹中的文件/图片,并将链接存储到我的数据库中的文件/图片中。我需要帮助才能实现这一点,因为即使我试图阅读很多相关内容,我也完全不了解如何实现这一点。 这是我的代码:

If($_POST['Submit'])
{

    global $wpdb;

    $medlemsnr=$_POST['medlemsnr'];
    $navn=$_POST['navn'];
    $baadnavn=$_POST['baadnavn'];
    $art=$_POST['art'];
    $vaegt=$_POST['vaegt'];
    $billede=$_POST['billede'];

    if($wpdb->insert(
                        'ct_storfanger_indberetninger',
                        array(
                        'Medlemsnr' => $medlemsnr,
                        'navn' => $navn,
                        'bådnavn' =>$baadnavn,
                        'art' =>$art,
                        'vægt' =>$vaegt,
                        'billede' =>$billede
                        )
                    ) == false) wp_die('Der var en fejl i indsendelsen. Kontakt venligst webmaster'); else echo 'Tak for din indsendelse. Den vil fremgå af listen så snart den er godkendt af juryen!<p />';

?>
<?php
}
else // else we didn't submit the form, so display the form
    {
?>

            <form action="" method="post" id="form">
            <table style="border:none;">
            <tr>
            <td style="border:none;width:25%">Medlemsnr</td>
            <td style="border:none;"><input name="medlemsnr" type="text" value="" /></td>
            </tr>
            <tr>
            <td style="border:none;">Navn</td>
            <td style="border:none;"><input name="navn" type="text" value="" /></td>
            </tr>
            <tr>
            <td style="border:none;">Bådnavn</td>
            <td style="border:none;"><input name="baadnavn" type="text" value="" /></td>
            </tr>
            <tr>
            <td style="border:none;">Art</td>
            <td style="border:none;">
            <?php 
                global $wpdb;
                $retrieve_data = $wpdb->get_results( "SELECT Art FROM ct_storfanger_arter");
            ?>
            <SELECT NAME="art"><option value=""></option>
            <?php
                foreach ($retrieve_data as $retrieved_data){ 
                    echo "<option value='" . $retrieved_data->Art . "'>" . $retrieved_data->Art . "</option>";
                }
            ?>
            </SELECT></td>
            </tr>
            <tr>
            <td style="border:none;">Vægt i gram</td>
            <td style="border:none;"><input name="vaegt" type="text" value="" /> gram</td>
            </tr>
            <tr>
            <td style="border:none;">Billede</td>
            <td style="border:none;"><input name="billede" type="file" value="" /></td>
            </tr>
            </table>
            <table style="border:none;">
            <tr>
            <td style="border:none;"><input type="submit" name="Submit" id="formsubmit" value="Indsend" /></form></td>
            </tr>
            </table>

            </form>
<?php
    } // end else no post['submit']
?>              

1 个答案:

答案 0 :(得分:0)

您可以使用此代码作为参考,以下是HTML:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

这将在PHP方面进行:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

只需在分离的文件夹上尝试此操作,直到您理解了代码,然后您就可以将其实现到您的wordpress修改中

如果你在该模块上加载wp_init,你也可以使用wordpress函数wp_handle_upload:

if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

$uploadedfile = $_FILES['file'];

$upload_overrides = array( 'test_form' => false );

$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

if ( $movefile && ! isset( $movefile['error'] ) ) {
    echo "File is valid, and was successfully uploaded.\n";
    var_dump( $movefile );
} else {
    /**
     * Error generated by _wp_handle_upload()
     * @see _wp_handle_upload() in wp-admin/includes/file.php
     */
    echo $movefile['error'];
}

这里是功能参考:wp_handle_upload