我不想创建像个人资料图片上传系统这样的 facebook 我需要它用于项目可以使用 jquery ajax 调用 php 文件并将上传的文件移动到选定的文件夹中并将图片位置插入数据库并将数据输出到选定的 div ? 任何形式的帮助将不胜感激。
下面是我的代码
索引.php
<html>
<head>
<title>
Member Area
</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#register").click( function() {
$.post( $("#form").attr("action"), $("#form :input").serializeArray(), function(info) { $("#results").empty(); $("#results").html(info)} );
});
$("#form").submit(function(){
return false;
});
});
</script>
</head>
<body>
<form name="form" id="form" enctype="multipart/form-data" action="php_file.php" method="POST">
<br /><input type="file" name="file" id="file"/>
<button name="submit" id="register">Register</button>
</form>
<div id="results"></div>
</body>
</html>
php_file.php
<?php
error_reporting( E_ALL ^ E_NOTICE);
session_start();
$_SESSION['username'] = "user1";
include_once("db.php");
$file = $_FILES['file']['name'];
$tmp_file = $_FILES['file']['tmp_name'];
if($file)
{
$location = "avatars/$file";
move_uploaded_file($tmp_file,$location);
$query = "UPDATE users SET image_location ='".$location."' WHERE username ='".$_SESSION['username']."'";
$result = mysql_query($query) or die (mysql_error());
$query2 = "SELECT image_location FROM users WHERE username ='".$_SESSION['username']."'";
$result2 = mysql_query($query2) or die (mysql_error());
$row = mysql_fetch_assoc($result2);
$image_location = $row['image_location'];
echo "<img src='".$image_location."' height='200px' width='200px'>";
}
else
{
$errors = "<div id='errors'>You must choose image with jpg , jpeg , png , gif this field cannot empty.</div>";
}
if(isset($errors)) { echo $errors; }
?>