我想通过商店图像路径创建一个图像上传到数据库的php表单,并将图像存储到服务器或目录中。我有点不确定如何进行图片上传。
数据库:CREATE TABLE IF NOT EXISTS `tbl_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fullName` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
'pic' blob NOT NULL
PRIMARY KEY (`id`),
<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
if(isset($_POST['btn-register']))
{
$fullName = $_POST['fullName'];
$email = $_POST['email'];
$stmt = $user_home->runQuery("SELECT * FROM bike WHERE id=:id");
$stmt->execute(array(":id"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);;
if($reg_user->register($email,$fullName))
{
$message = "
Thank You for registering with us!<br/>
<br />
Thanks,<br/>
<br />
Site Admin";
$subject = "Confirm Registration";
}
else
{
echo "sorry , query could no execute.";
}
}
?>
<!DOCTYPE html>
<html class="no-js">
<head>
<title><?php echo $row['email']; ?></title>
<!-- Bootstrap CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/bootstrap-theme.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="../css/NewFile.css">
</head>
<body>
<script src="../js/jquery-1.12.3.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<?php include 'navBarLogin.php'; ?>
<?php if(isset($msg)) echo $msg; ?>
<form method="POST" action="" enctype="multipart/form-data">
<h2> Bicycle Register</h2>
<hr>
<table>
<tr>
<td>Full name:</td>
<td><input type="text" class="input-block-level" name="fullName" /></td>
</tr>
<tr>
<td>email:</td>
<td><input type="text" class="input-block-level" name="email" /></td>
</tr>
<tr>
<td>SPicture:</td>
<td><input type="file" name="pic" required />
</tr>
</table>
<button class="btn btn-large btn-primary" type="submit" name="btn-register">Register</button>
</form>
</body>
</html>
PHP:
import java.io.IOException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonToJava {
public static void main(String[] args) throws IOException {
try{
String json = "<YOUR_JSON>";
Gson gson = new GsonBuilder().create();
VendorInfo vInfo = gson.fromJson(json, VendorInfo.class);
System.out.println(vInfo.getVendorName());
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
基于http://www.codingcage.com/2015/09/login-registration-email-verification-forgot-password-php.html
创建的网站答案 0 :(得分:0)
这一行之后:
$email = trim($_POST['email']);
为图片上传添加以下代码:
...
if(isset($_FILES['pic'])){
$errors= array();
$file_name = $_FILES['pic']['name'];
$file_size = $_FILES['pic']['size'];
$file_tmp = $_FILES['pic']['tmp_name'];
$file_type = $_FILES['pic']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['pic']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
...
然后你必须在你的数据库中插入$ file_name。
答案 1 :(得分:0)
您可以将文件上传到您的文件夹,如下所示:
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
/*add here the insert on your tabl*/
} else {
echo "Sorry, there was an error uploading your file.";
}
答案 2 :(得分:0)
<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
if(isset($_POST['btn-register']))
{
$fullName = $_POST['fullName'];
$email = $_POST['email'];
if(isset($_FILES['pic'])){
$errors= array();
$file_name = $_FILES['pic']['name'];
$file_size = $_FILES['pic']['size'];
$file_tmp = $_FILES['pic']['tmp_name'];
$file_type = $_FILES['pic']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['pic']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152) {
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
$stmt = $user_home->runQuery("SELECT * FROM bike WHERE id=:id");
$stmt->execute(array(":id"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);;
if($reg_user->register($email,$fullName))
{
$message = "
Thank You for registering with us!<br/>
<br />
Thanks,<br/>
<br />
Site Admin";
$subject = "Confirm Registration";
}
else
{
echo "sorry , query could no execute.";
}
}
?>
<!DOCTYPE html>
<html class="no-js">
<head>
<title><?php echo $row['email']; ?></title>
<!-- Bootstrap CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/bootstrap-theme.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="../css/NewFile.css">
</head>
<body>
<script src="../js/jquery-1.12.3.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<?php include 'navBarLogin.php'; ?>
<?php if(isset($msg)) echo $msg; ?>
<form method="POST" action="" enctype="multipart/form-data">
<h2> Bicycle Register</h2>
<hr>
<table>
<tr>
<td>Full name:</td>
<td><input type="text" class="input-block-level" name="fullName" /></td>
</tr>
<tr>
<td>email:</td>
<td><input type="text" class="input-block-level" name="email" /></td>
</tr>
<tr>
<td>SPicture:</td>
<td><input type="file" name="pic" required />
</tr>
</table>
<button class="btn btn-large btn-primary" type="submit" name="btn-register">Register</button>
</form>
</body>
</html>