溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ajax如何上傳多圖到php服務(wù)器

發(fā)布時(shí)間:2021-04-09 17:35:53 來源:億速云 閱讀:191 作者:啵贊 欄目:web開發(fā)

本篇內(nèi)容介紹了“ajax如何上傳多圖到php服務(wù)器”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)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 > </span><div class="bookImg"> 
    <div class="img-box"> 
      <input type="file" name="photo1" id="" title="文件不超過200kb,大小最佳為60*60"> 
    </div> 
    <div class="img-box"> 
      <input type="file" name="photo2" id="" title="文件不超過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(); 
    $path2 = "img/" . $filename1; <span >//注意要在目錄下新建一個(gè)名為img的文件夾用來存放圖片 
    $path3 = "img/" . $filename2; 
    if(move_uploaded_file($_FILES['photo1']['tmp_name'], $path2) && move_uploaded_file($_FILES['photo2']['tmp_name'], $path3) ){            
      $response['isSuccess'] = true;   
      $response['photo1'] = $path2;  
      $response['photo2'] = $path3;       
    }else{  
      $response['isSuccess'] = false;  
    }  
    echo json_encode($response); 
?>

數(shù)據(jù)庫(kù)表我就不貼了,存圖片地址,字段類型直接用字符型就可以了。

現(xiàn)在在介紹一下把圖片轉(zhuǎn)換成二進(jìn)制直接存進(jìn)數(shù)據(jù)庫(kù)的方法:

這里我沒有用ajax請(qǐng)求,直接用表單的post 請(qǐng)求提交數(shù)據(jù)

html代碼:

<form action="photo.php"> 
<span > </span><div class="pic"> 
    <input type="file" name="photo" id="" title="文件不超過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ù)了。

“ajax如何上傳多圖到php服務(wù)器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI