gpt4 book ai didi

php - 图像上传到mysql数据库不起作用

转载 作者:行者123 更新时间:2023-11-29 12:08:31 29 4
gpt4 key购买 nike

我正在使用 jquery filedialog 加载图像文件。然后向 php 文件发送 ajax 请求,将图像上传到数据库。这是我发送的 ajax 请求

$("#imgfile").on("change",function(e){
var file_data = $('#imgfile').prop('files')[0];
e.preventDefault();
var form_data = new FormData();
form_data.append('file', file_data);
//alert(form_data);
$.ajax({
url: 'changeProfileImg.php', // point to server-side PHP script
contentType: "image/jpeg", // what to expect back from the PHP script, if anything
cache: false,
dataType: "image",
processData: false,
data: form_data,
type: 'post',
success: function(php_script_response){
alert(php_script_response); // display response from the PHP script, if any
}
});
});

这是我的 php 代码

include_once 'dbconnect.php';
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
//move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
$file=$_FILES['file']['name'];
$id=$_SESSION['id'];
$sql="UPDATE mainadmin set photo='$file' where id='$id'";
mysql_query($sql);
echo 'success';
$_SESSION['photo']=$file;
}

但我看到一些垃圾内容被插入数据库中而不显示图像。我怎样才能让它工作。

最佳答案

请参阅下面的网址和代码,我认为这对您有帮助。

Simple jQuery progress bar percentage

这是我测试的代码文件上传,带有进度条百分比

但您可以根据您的要求进行管理或修改。

<!doctype html>
<head>
<title>File Upload Progress Demo #1</title>
<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }

.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
<h1>File Upload Progress Demo #1</h1>
<code>&lt;input type="file" name="myfile"></code>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedfile"><br>
<input type="submit" value="Upload File to Server">
</form>

<div class="progress">
<div class="bar"></div >
<div class="percent">0%</div >
</div>

<div id="status"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function(xhr) {
bar.width("100%");
percent.html("100%");
status.html(xhr.responseText);
}
});

})();
</script>

</body>
</html>

我的PHP代码

<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>

关于php - 图像上传到mysql数据库不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085709/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com