ajax 异步插入图片到数据库(单图上传)

   其实也没啥  如图:javascript

点击按钮选择图片,选择完成后 无需点击肯定 ,自动上传到服务器指定文件夹 而后插入到数据库中。php

下面来看看这要代码css

index.phphtml

 


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>UploadiFive Test</title>
<script src="//cdn.bootcss.com/jquery/2.0.0/jquery.min.js" type="text/javascript"></script>
<script src="jquery.uploadify.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="uploadify.css">
<style type="text/css">
body {
font: 13px Arial, Helvetica, Sans-serif;
}
</style>
</head>

<body>
<h1>Uploadify Demo</h1>
<form action="uploadify.php" method="post">
<div id="queue"></div>
<input id="file_upload" name="tupian" type="file" multiple="true">
<!-- <input type="submit" value="上传"/>-->
</form>

<script type="text/javascript">
<?php $timestamp = time();?>
$(function() {
$('#file_upload').uploadify({
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>'
},
'swf' : 'uploadify.swf',
'uploader' : 'uploadify.php'
});
});
</script>
</body>
</html>

上传页面的 uploadifty.php
<?php
include("conn.php");
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
error_reporting(0);
// Define a destination
$targetFolder = '/DoExercise/uploadify/uplaods'; // Relative to the root

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
$tupian=$_FILES['Filedata']['name'];
$sql="insert into uploadify(tupian) values('$tupian')";
$query=mysql_query($sql);
if($query>0){
echo "<script>alert('上传成功')</script>";
}else{

echo"<script>alert('上传失败')</script>";
}

}
?>
,$fileTypes)) { move_uploaded_file($tempFile,$targetFile); echo '1'; } else { echo 'Invalid file type.'; } $tupian=$_FILES['Filedata']['name']; $sql="insert into uploadify(tupian) values('$tupian')"; $query=mysql_query($sql); if($query>0){ echo "<script>alert('上传成功')</script>"; }else{ echo"<script>alert('上传失败')</script>"; }}?>主要就这些玩意 也没啥 东西 须要文件的 找我来哈 认准大白驴。