我想做的是制作一个图片上传系统并将图片发送到upload
文件夹并将链接保存到数据库。
我的问题是我的错误images.php
我可以帮助我。
HTML:
<form method="post" enctype="multipart/form-data">
<img id="picture" data-src="#" /> <br />
<input type='file' name="image" id="imgInp" accept="image/*" /><br />
<input type="submit" name="submit" id="submit" value="submit" />
</form>
脚本:
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function (e) {
e.preventDefault();
var data = {};
data.image = $('#imgInp').val();
$.ajax({
type: "POST",
url: "images.php",
data: data,
cache: false,
success: function (response) {
}
});
return false;
});
});
</script>
images.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$image = addslashes(file_get_contents(@$_FILES['image']['tmp_name']));
$image_name = addslashes(@$_FILES['image']['name']);
$image_size = getimagesize(@$_FILES['image']['tmp_name']);
move_uploaded_file(@$_FILES["image"]["tmp_name"], "upload/" . @$_FILES["image"]["name"]);
$location = "upload/" . @$_FILES["image"]["name"];
$q = "INSERT INTO students( image ) VALUES( :image)";
$query = $dbc->prepare($q);
$query->bindParam(':image', $location);
$results = $query->execute();
?>
用于图片上传的脚本:
<script type="text/javascript">
$(document).ready(function() {
var currentSrc = $('#picture').attr('src');
if(currentSrc==null || currentSrc==""){
$('#picture').attr('src','http://i38.photobucket.com/albums/e149/eloginko/profile_male_large_zpseedb2954.jpg');
$("#picture").on('click', function() {
$("#imgInp").trigger('click')}
)}
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#picture').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
});
</script>
答案 0 :(得分:0)
摆脱错误消息最简单的方法是实际放置一个条件,检查$_FILES
中是否有任何内容。但过去那个,不清楚$FILES
空洞的根本原因。根据我的经验,在另一方面使用PHP接收器上传Ajax文件时,最好不能始终如一地工作。无论如何,这是我的代码版本,有条件的地方:
if (!empty($_FILES)) {
$image = addslashes(file_get_contents(@$_FILES['image']['tmp_name']));
$image_name = addslashes(@$_FILES['image']['name']);
$image_size = getimagesize(@$_FILES['image']['tmp_name']);
move_uploaded_file(@$_FILES["image"]["tmp_name"], "upload/" . @$_FILES["image"]["name"]);
$location = "upload/" . @$_FILES["image"]["name"];
$q = "INSERT INTO students( image ) VALUES( :image)";
$query = $dbc->prepare($q);
$query->bindParam(':image', $location);
$results = $query->execute();
}
答案 1 :(得分:0)
尝试这种方法,if语句可能看起来太多了,但如果你想要可靠的代码,你需要检查:
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$folder = "upload/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
echo "succesful upload, we have an image!";
//PDO
$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "INSERT INTO students( image ) VALUES( :image)";
$stmt = $dbc->prepare($query);
$stmt->bindParam(':image', $full_path);
$results = $stmt->execute();
if($results){
echo "Insert successful!";
}else{
echo "Insert failed!";
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
我想指出几点:
file_get_contents
的使用尚不清楚$image*
变量? ....