上传文件到数据库PHP/WordPress
问题描述:
我已经创建了这个PHP代码上传文件到我的数据库(phpMyAdmin),但我不知道如何使它在WordPress上工作。上传文件到数据库PHP/WordPress
$con = mysqli_connect("localhost","root","","mydb");
if (mysqli_connect_errno()) {
echo "Unable to connect to MySQL! ". mysqli_connect_error();
}
if (isset($_POST['submit'])) {
$target_dir = get_template_directory_uri() .'/Uploaded_Files';
$target_file = $target_dir . date("dmYhis") . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "jpg" || $imageFileType != "png" || $imageFileType != "jpeg" || $imageFileType != "gif") {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
$files = date("dmYhis") . basename($_FILES["file"]["name"]);
} else {
echo "Error Uploading File";
exit;
}
} else {
echo "File Not Supported";
exit;
}
$filename = $_POST['filename'];
$location = get_template_directory_uri() .'/Uploaded_Files . $files';
$getuser = $_SESSION['id'];
$settime = date("Y-m-d H:i:s");
$setmsg = $con->real_escape_string($_POST['msg']);
// Database Query goes here.
}
答
通常当我们导出数据库从在WordPress站点它显示了这样的代码,它提供了有关WordPress插件页面等等等等表的信息
-- Table structure for table `wp_commentmeta`
--
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
`meta_id` bigint(20) unsigned NOT NULL,
`comment_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`meta_key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`meta_value` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
AT the head of exported sql sheet it shows...
-- phpMyAdmin SQL Dump
-- version 4.3.8
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Oct 10, 2017 at 11:57 AM
-- Server version: 5.5.51-38.2
-- PHP Version: 5.6.20
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: database name
答
首先,当工作与WordPress数据库不使用连接,如mysqli_connect
,因为WordPress有自己的API与MySQL进行交互,官方文档:https://codex.wordpress.org/Class_Reference/wpdb。
您约“WordPress数据库上传文件”说,我想你的意思是将其添加到媒体库的方式,在这种情况下,本教程应该帮助https://rudrastyh.com/wordpress/how-to-add-images-to-media-library-from-uploaded-files-programmatically.html
_注意:_ phpMyAdmin是不是一个数据库,它是一个基于Web的管理MySQL数据库的工具。 –
https://codex.wordpress.org/Uploading_Files – jdv
哦!我不是指PHPmyadmin作为数据库。无论如何。 – devsam247