ajax上傳多圖到php服務(wù)器的方法
一般上傳圖片到服務(wù)器有兩種方式:
1、把圖片轉(zhuǎn)換成二進(jìn)制直接存儲(chǔ)到數(shù)據(jù)庫(kù)里
2、把圖片存儲(chǔ)到本地目錄,并將圖片地址存儲(chǔ)到數(shù)據(jù)庫(kù)里
先粗淺地談下我對(duì)這兩種存儲(chǔ)方法的優(yōu)劣點(diǎn)的認(rèn)識(shí):
1、把圖片轉(zhuǎn)換成二進(jìn)制直接存儲(chǔ)到數(shù)據(jù)庫(kù)的優(yōu)點(diǎn)是有利于數(shù)據(jù)的備份和遷移,但缺點(diǎn)就是會(huì)影響數(shù)據(jù)讀寫速率。一般大圖、多圖不建議用此方式,一般存儲(chǔ)用戶頭像、富文本內(nèi)容存儲(chǔ)時(shí)可以應(yīng)用此方式。
2、將圖片存儲(chǔ)到本地目錄,在數(shù)據(jù)庫(kù)上只存儲(chǔ)圖片路徑的優(yōu)點(diǎn)是有利于數(shù)據(jù)的讀寫,畢竟存一個(gè)地址要比存整個(gè)圖片的大小要小得多。但是缺點(diǎn)就不利于數(shù)據(jù)的備份和遷移。
先介紹一下存儲(chǔ)圖片路徑的方法:
html代碼:
<form id="form1">
<span style="white-space:pre;"> </span><div class="bookImg">
<div class="img-box">
<input type="file" name="photo1" id="" title="文件不超過(guò)200kb,大小最佳為60*60">
</div>
<div class="img-box">
<input type="file" name="photo2" id="" title="文件不超過(guò)200kb,大小最佳為60*60">
</div>
</div>
<input type="button" class="bookBtn btnBlue" id="publishBook" value="發(fā)布圖書" onclick="fsubmit()"/>
</form>
ajax請(qǐng)求:
function fsubmit() {
var form1=document.getElementById("form1");
var fd =new FormData(form1);
$.ajax({
url: "photo.php",
type: "POST",
data: fd,
processData: false,
contentType: false,
success: function(response,status,xhr){
console.log(xhr);
var json=$.parseJSON(response);
var result = '';
result += '<br/><img src="' + json['photo1'] + '" height="100" />';
result += '<br/><img src="' + json['photo2'] + '" height="100" />';
result += '<br/>' + json['photo1'];
result += '<br/>' + json['photo2'];
$('#result').html(result);
}
});
return false;
}
php代碼:photo.php
<?php
require('conn.php');
$nameTag = time();
$filename1 = $nameTag . '0' . substr($_FILES['photo1']['name'], strrpos($_FILES['photo1']['name'],'.'));
$filename2 = $nameTag . '1' . substr($_FILES['photo2']['name'], strrpos($_FILES['photo2']['name'],'.'));
$response = array();
$path1 = "img/" . $filename1; <span style="color:#ff0000;">//注意要在目錄下新建一個(gè)名為img的文件夾用來(lái)存放圖片
$path2 = "img/" . $filename2;
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $path1) && move_uploaded_file($_FILES['photo2']['tmp_name'], $path2) ){
$response['isSuccess'] = true;
$response['photo1'] = $path1;
$response['photo2'] = $path2;
}else{
$response['isSuccess'] = false;
}
echo json_encode($response);
?>
數(shù)據(jù)庫(kù)表我就不貼了,存圖片地址,字段類型直接用字符型就可以了。
現(xiàn)在在介紹一下把圖片轉(zhuǎn)換成二進(jìn)制直接存進(jìn)數(shù)據(jù)庫(kù)的方法:
這里我沒(méi)有用ajax請(qǐng)求,直接用表單的post 請(qǐng)求提交數(shù)據(jù)
html代碼:
<form action="photo.php">
<span style="white-space:pre;"> </span><div class="pic">
<input type="file" name="photo" id="" title="文件不超過(guò)200kb,大小最佳為60*60" onchange="imgPreview(this)">上傳頭像
</div>
</form>
php代碼:photo.php
<?php
require('conn.php');
$image = mysql_real_escape_string(file_get_contents($_FILES['photo']['tmp_name']));
$sqlstr = "insert into user(photo) values('".$image."')";
@mysql_query($sqlstr) or die(mysql_error());
exit();
?>
這樣就把圖片轉(zhuǎn)換成二進(jìn)制并儲(chǔ)存進(jìn)數(shù)據(jù)庫(kù)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ajax實(shí)現(xiàn)異步文件或圖片上傳功能
- 基于ajax實(shí)現(xiàn)文件上傳并顯示進(jìn)度條
- 利用ajaxfileupload插件實(shí)現(xiàn)文件上傳無(wú)刷新的具體方法
- 使用ajaxfileupload.js實(shí)現(xiàn)ajax上傳文件php版
- ajaxFileUpload.js插件支持多文件上傳的方法
- ajax(iframe)無(wú)刷新提交表單、上傳文件
- ajax 文件上傳應(yīng)用簡(jiǎn)單實(shí)現(xiàn)
- AJAX和JSP實(shí)現(xiàn)的基于WEB的文件上傳的進(jìn)度控制代碼
- Ajax方式提交帶文件上傳的表單及隱藏iframe應(yīng)用
相關(guān)文章
Ajax實(shí)現(xiàn)phpcms 點(diǎn)贊功能實(shí)例代碼
這篇文章主要介紹了Ajax實(shí)現(xiàn)phpcms 點(diǎn)贊功能實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
ajax實(shí)現(xiàn)數(shù)據(jù)刪除、查看詳情功能
本文主要介紹了ajax實(shí)現(xiàn)數(shù)據(jù)刪除、查看詳情功能,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
AJAX淺析數(shù)據(jù)交換的實(shí)現(xiàn)
在AJAX中,最常用的就是JSON,XML因?yàn)楸容^冗雜所以用的比較少。所以我們先來(lái)說(shuō)基于JSON的數(shù)據(jù)交換。最后我們還會(huì)提到在數(shù)據(jù)交換中出現(xiàn)亂碼的形式2022-08-08
Ajax輪詢請(qǐng)求狀態(tài)(微信公眾號(hào)帶參數(shù)二維碼登錄網(wǎng)站)
最近做了一個(gè)項(xiàng)目,其中有功能要求通過(guò)掃碼微信公眾號(hào)帶參數(shù)的二維碼來(lái)登錄網(wǎng)站,接下來(lái)小編給大家介紹實(shí)現(xiàn)思路及代碼,一起看看吧2016-09-09
空格或者空白字符導(dǎo)致$.ajax()報(bào)parseerror錯(cuò)誤小結(jié)
這篇文章主要介紹了空格或者空白字符導(dǎo)致$.ajax()報(bào)parseerror錯(cuò)誤,需要的朋友可以參考下2014-04-04
強(qiáng)烈推薦 - Ajax 技術(shù)資源中心
強(qiáng)烈推薦 - Ajax 技術(shù)資源中心...2007-05-05

