作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在这里上传两个代码,一个是 Index.php,另一个是 ImageUpload.php,当我上传图像时,文件被移动到上传文件夹中,图像名称两次存储在数据库中,我只想在数据库中存储一个数据我上传的每张图片都是数据库
images | int(11)
name | varchar(200)
image | longtext
<?php
include("config.php");
if(isset($_POST) && !empty($_FILES['image']['name'])){
$valid_extensions = array('jpeg', 'jpg', 'png','pdf','doc','docx'); // valid extensions
$filepath = 'uploads/';
if(!is_dir($filepath)){
mkdir($filepath, 0755, true);
}
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
list($txt, $ext) = explode(".", $img); // alternative $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
$image_name = time().".".$ext;
$image_base64 =md5(rand());
$image = 'data:image/'.$image_name.';base64,'.$image_base64;
$query = "insert into images(name,image) values('".$image_name."','".$image."')";
mysqli_query($con,$query) or die(mysqli_error($con));
// Validate File extension
if(in_array($ext, $valid_extensions))
{
$filepath=$filepath.$image_name;
if(move_uploaded_file($tmp,$filepath)){
echo "<img width='100px' src='".$filepath."'>";
}else{
echo "Failed to upload image on server";
}
}else
{
echo 'Please upload valid image';
}
}else{
echo "Please select image to upload";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Ajax image upload without refreshing page with validation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-offset-2 col-md-8">
<h2>PHP - Upload image on server with validation using jQuery Ajax</h2>
<div class="panel panel-info">
<div class="panel-heading">File upload using ajax in PHP</div>
<div class="panel-body">
<form action="imageUpload.php" enctype="multipart/form-data" class="form-horizontal" method="post">
<div class="preview"></div>
<input type="file" name="image" class="form-control" />
<br/>
<button class="btn btn-block btn-primary btn-upload" id="upload" name="upload">Upload</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$(".btn-upload").click(function(){
$(".form-horizontal").ajaxForm({target: '.preview'}).submit();
});
});
</script>
</body>
</html>
最佳答案
这是因为您的 ajax 请求运行了两次。只需在表单元素上添加 onsubmit="return false;"
即可。使得表单仅通过ajax代码提交。
<form action="imageUpload.php" enctype="multipart/form-data" class="form-horizontal" method="post" onsubmit="return false;">
<div class="preview"></div>
<input type="file" name="image" class="form-control" />
<br/>
<button class="btn btn-block btn-primary btn-upload" id="upload" name="upload">Upload</button>
</form>
关于javascript - 当我只上传一次时,图像在表格中插入了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779800/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!