想要创建一个简单的表单来将值和图像发送到数据库,下面是php代码 -
创建了两个php文件process_upload和connection
processs_upload.php
<?php
require 'connection.php';
$conn = Connect();
$name=$conn->real_escape_string($_POST['name']);
$mobile_number=$conn->real_escape_string($_POST['mobile_number']);
$email_id=$conn->real_escape_string($_POST['email_id']);
$type=$conn->real_escape_string($_POST['type']);
$query = "INSERT into tb_cform (name,mobile_number,email_id,type) VALUES('" . $name . "','" . $mobile_number . "','" . $email_id . "','" . $type . "')";
if ( 0 < $_FILES['image_file']['error'] ) {
echo 'Error: ' . $_FILES['image_file']['error'] . '<br>';
}
else{
$time=time();
if(($type=='image/png') || ($type=='image/gif') || ($type=='image/jpeg') || ($type=='image/pjpeg') || ($type=='image/jpg')){ //if file is image
if(move_uploaded_file($_FILES['image_file']['tmp_name'], 'pictures/'.$time.'_'.$_FILES['image_file']['name'])){
echo 'done ';
}else{
echo 'image error';
}
}else{//if file is video
if(move_uploaded_file($_FILES['image_file']['tmp_name'], 'videos/'.$time.'_'.$_FILES['image_file']['name'])){
echo 'done';
}else{
echo 'video error';
}
}
}
$conn->close();
?>
connection.php
<?php
function Connect()
{
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "";
$dbname = "database";
// Create connection
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);
return $conn;
}
?>
在phpmyadmin中创建了一个表,下面是表 -
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `tb_cform` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`name` text CHARACTER SET utf8 NOT NULL,
`mobile_number` int(11) NOT NULL,
`email_id` text NOT NULL,
`type` longblob NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
HTML表单
<!DOCTYPE HTML>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="Boomer" />
<title>Uploader</title>
<link href="css/upload_css.css" media="screen" rel="stylesheet" type="text/css" />
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.form.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var optionsH = {
target: '#H_output', // target element(s) to be updated with server response
beforeSubmit: beforeSubmitH, // pre-submit callback
uploadProgress: OnProgressH,
error: onerrorH,
success: afterSuccessH, // post-submit callback
resetForm: true // reset the form after successful submit
};
$('#MyUploadFormH').submit(function(){
var name=$("#name").val();
var mobile_number=$("#mobile_number").val();
var email_id=$("#email_id").val();
if((name!='') && (mobile_number!='') && (email_id!='')){
$('#MyUploadFormH').attr("action","process_upload.php");
$(this).ajaxSubmit(optionsH);
// return false to prevent standard browser submit and page navigation
return false;
}else{
$("#H_output").html("All field are required");
}
});
function beforeSubmitH(){
//check whether browser fully supports all File API
if (window.File && window.FileReader && window.FileList && window.Blob)
{
if(!$('#imageInputH').val()) //check eHty input filed
{
$("#H_output").html("select picture or video to upload");
return false
}else{
$("#H_output").html("file selected");
}
var fsize = $('#imageInputH')[0].files[0].size; //get file size
var ftype = $('#imageInputH')[0].files[0].type; // get file type
switch(ftype)
{
case 'image/png': case 'image/gif': case 'image/jpeg': case 'image/pjpeg': case 'video/mp4': case 'video/3gp': case 'video/avi': case 'video/mkv':
case 'video/flv': case 'video/wmv': case 'video/ovb': case 'video/ogg':
break;
default:
$("#H_output").html("<b>"+ftype+"</b> Unsupported file type!");
return false
}
//Allowed file size is less than 1 MB (1048576)
if(fsize>10485760)
{
$("#H_output").html("<b>"+bytesToSize(fsize) +"</b> is too big! <br />Please reduce the size and try again.");
return false
}
document.getElementById("header_submit-btn").setAttribute("disabled", true);
$("#H_output").html("");
}
else
{
//Output error to older unsupported browsers that doesn't support HTML5 File API
$("#H_output").html("Please upgrade your phone!");
return false;
}
}
function OnProgressH(){
$("#upload_formH").hide();
$("#upload_loader_header").show();
}
//when error occur
function onerrorH(){
document.getElementById("header_submit-btn").removeAttribute("disabled", true);
$("#H_output").html("connection error...");
//$('#staff_bfr_uplad_prv_cnt').html("");
//progressboxH.hide();
//$('#MyUploadForm').resetForm();
$("#upload_formH").show();
$("#upload_loader_header").hide();
}
//after succesful upload
function afterSuccessH()
{
document.getElementById("header_submit-btn").removeAttribute("disabled", true);
$("#upload_formH").show();
$("#upload_loader_header").hide();
var logo_thumb=$("#header_pix_location").html();
$("img[id^='eml_header_logo']").attr('src',dp_thumb);
}
document.querySelector('#H_fileSelect').addEventListener('click', function(e){
var fileInput = document.querySelector('#imageInputH');
//clickH(fileInput); // Simulate the click with a custom event.
fileInput.click(); // Or, use the native click() of the file input.
$("#H_output").html("");
}, false);
$("#imageInputH").change(function(){
var fsize = $('#imageInputH')[0].files[0].size; //get file size
var ftype = $('#imageInputH')[0].files[0].type; // get file type
if(fsize>10485760){
$("#H_output").html("<b>"+bytesToSize(fsize) +"</b> is too big! <br />Please reduce the size and try again.");
document.getElementById("header_submit-btn").setAttribute("disabled", true);
}else{
$("#type").val(ftype);
$("#H_output").html("<span style='color: green;'>file selected</span>");
document.getElementById("header_submit-btn").removeAttribute("disabled", true);
}
});
//function to format bites bit.ly/19yoIPO
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Bytes';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}
});
</script>
</head>
<body>
<div id="add_cpp_form">
<center>
<div style="float: none; width: 270px; padding: 10px;" id="upload_formH">
<form onSubmit="return false" method="post" enctype="multipart/form-data" id="MyUploadFormH">
<input type="text" class="form_input" id="name" name="name" value="" placeholder="Name" />
<input type="text" class="form_input" id="mobile_number" name="mobile_number" value="" placeholder="Mobile Number" />
<input type="text" class="form_input" id="email_id" name="email_id" value="" placeholder="Email Id" />
<input name="image_file" id="imageInputH" type="file" />
<input name="type" id="type" type="hidden" />
<input type="submit" id="header_submit-btn" class="effect1 ripple" value="Submit" />
<div id="H_fileSelect" class="effect1 ripple" style="border-radius: 0px;">Select</div>
<img src="images/ajax-loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/>
</form>
<div id="H_output"></div>
</div>
<div id="upload_loader_header" style="float: left; display: none; text- align: center; width: 100%;">
<img style="float: none;" src="img/ajax-loader.gif" width="32" height="32" />
</div>
</center>
</div>
</body>
</html>
现在,当我运行代码时,我收到图像错误和视频错误消息,连接成功完成但收到消息。
答案 0 :(得分:0)
尝试授予权限,如
假设您的代码位于/ var / www / my_project
下尝试chmod -R 664 / var / www / my_project。