本文目錄一覽:
php上傳圖片文件常用的幾個方法
你好,要先建立一個html代碼
form action=”upload_file.php” method=”post”
enctype=”multipart/form-data”
label for=”file”Filename:/label
input type=”file” name=”file” id=”file” /
br /
input type=”submit” name=”submit” value=”Submit” /
/form
然後創建upload_file文件用$_FILE判斷文件,下面是判斷文件的具體信息
$_FILES[“file”][“name”] – 被上傳文件的名稱
$_FILES[“file”][“type”] – 被上傳文件的類型
$_FILES[“file”][“size”] – 被上傳文件的大小,以字節計
$_FILES[“file”][“tmp_name”] – 存儲在服務器的文件的臨時副本的名稱
$_FILES[“file”][“error”] – 由文件上傳導致的錯誤代
希望對你有幫助!
php 如何上傳圖片到我指定的文件夾.
使用這個函數move_uploaded_file ($filename,$path);使用範例: if(!empty($_FILES[“magfile”])) {
$uploaddir = $_SERVER[‘DOCUMENT_ROOT’].”/uploads/”;
$uploaddir.=”test.jpg”;
if(move_uploaded_file($_FILES[“magfile”][“tmp_name”], $uploaddir)) {
echo “上傳成功!”;
}else{
print_r($_FILES);
}
}第一個參數是上傳到服務器臨時文件夾的絕對路徑,$_FILES[‘文件域名稱’][‘tmp_name’]第二個參數是需要放到服務器上的絕對路徑+文件名。覺得好請採納~
php 編寫 實現上傳圖片至服務器的函數
?php
class FileUpload{
private $filepath; //指定上傳文件保存的路徑
private $allowtype=array(“gif”,”jpg”,”jpeg”,”png”);//允許上傳文件的類型
private $maxsize=1000000;//允許上傳文件的最大值
private $israndname=true;//是否隨機重命名,
private $originName;//源文件名字
private $tmpFileName;//臨時文件名字
private $fileType;//上傳後的文件類型,主要是文件後綴名
private $fileSize;//文件尺寸
private $newFileName;//新文件名字
private $errorName=0;//錯誤號
private $errorMess=””;//用來提供錯誤報告
//用於對上傳文件初始化
//指定上傳路徑 2·允許的類型 3·限制大小 4·是否使用隨機文件名稱
//讓用戶可以不用換位置傳參數,後面參數給值不用按照位置或者必須有值
function __construct($options=array()){
foreach($options as $key=$val){
$key = strtolower($key);
//查看用戶參數中的數組下標是否和成員屬性名相同
//get_class_vars(get_class($this))得到類屬性的數組
//如果$key下標不在這個類屬性的數組中,則退出for循環
if (!in_array($key,get_class_vars(get_class($this)))){
continue;
}
$this – setOption($key,$val);
}
}
private function setOption($key,$val){
//讓實例化後獲取過來的數組下標 = 數組下標的值,這裡即為構造函數初始化
//構造函數中調用,等於把所有屬性初始化,將來可以直接訪問
$this – $key=$val;
}
private function getError(){
$str=”上傳文件{$this-originName}時出錯”;
switch($this – errorNum){
case 4: $str.=”沒有文件被上傳”;
break;
case 3: $str.=”文件只有部分上傳”;
break;
case 2: $str.=”上傳文件超過了表單的值”;
break;
case 1: $str.=”上傳文件超過phpini的值”;
break;
case -1: $str.=”未允許的類型”;
break;
case -2: $str.=”文件過大上傳文件不能超過{$this-maxsize}字節”;
break;
case -3: $str.=”上傳文件失敗”;
break;
case -4: $str.=”建立存放上傳文件目錄失效,請重新上傳指定目錄”;
break;
case -5: $str.=”必須指定上傳文件的路徑”;
break;
default: $str.=”未知錯誤”;
}
return $str.’br’;
}
//用來檢查文件上傳路徑
private function checkFilePath(){
if(empty($this – filepath)){
$this – setOption(“errorNum”,-5);
return false;
}
if(!file_exists($this – filepath) || !is_writable($this – filepath)){
if(!@mkdir($this – filepath,0755)){
$this – setOption(“errorNum”,-4);
return false;
}
}
return true;
}
//用來檢查上傳文件尺寸大小
private function checkFileSize(){
if($this – fileSize $this -maxsize){
$this – setOption(“errorNum”,-2);
return false;
}else{
return true;
}
}
//用來檢查文件上傳類型
private function checkFileType(){
if(in_array(strtolower($this-fileType),$this – allowtype)){
return true;
}else{
//如果$this-fileType這個類型 不在$this – allowtype這個數組中,則把錯誤號變成-1
$this – setOption(“errorNum”,-1);
return false;
}
}
private function setNewFileName(){
if($this – israndname){
$this – setOption(“newFileName”,$this-preRandName());
}else{
$this – setOption(“newFileName”,$this – originName);
}
}
//用於檢查文件隨機文件名
private function preRandName(){
$fileName=date(“Ymdhis”).rand(100,999);
return $fileName.”.”.$this – fileType;
}
//用來上傳一個文件
function uploadFile($fileField){
//檢查文件路徑
$return = true;
if(!$this – checkFilePath()){
$this – errorMess=$this – getError();
return false;
}//獲取文件信息
$name = $_FILES[$fileField][‘name’];
$tmp_name = $_FILES[$fileField][‘tmp_name’];
$size = $_FILES[$fileField][‘size’];
$error = $_FILES[$fileField][‘error’];
if(is_array($name)){//判斷獲取過來的文件名字是否為數組
$errors=array();//如果為數組則設置為一個數組錯誤號
for($i=0;$icount($name);$i++){
//循環每個文件即每個類屬性賦值或者說初始化屬性值 或者初始化構造函數
if($this-setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
if(!$this-checkFileSize() || !$this-checkFileType()){
//如果上面尺寸或者類型不對,則調用這個錯誤信息
$errors[$i]=$this-getError();
$return=false;
}
}else{
//這裡是
$error[]=$this-getError();
$return=false;
}
if(!$return)
$this-setFiles();
}
if($return){
$fileNames=array();
for($i=0;$icount($name);$i++){
if($this-setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
$this-setNewFileName();
if(!$this-copyFile()){
$errors=$this-getError();
$return=false;
}else{
$fileNames[$i]=$this-newFileName;
}
}
}
$this-newFileName=$fileNames;
}
$this-errorMess=$errors;
return $return;
}else{
//看看$name,$tmp_name,$size,$error這些是否賦值成功 否則返回FALSE
if($this – setFiles($name,$tmp_name,$size,$error)){
//看看文件大小尺寸是否匹配,不匹配返回FALSE
if($this – checkFileSize() $this – checkFileType()){
//獲取新文件名
$this-setNewFileName();
if($this-copyFile()){
return true;
}else{
return false;
}
}else{
$return=false;
}
}else{
$return=false;
}
if(!$return){
$this – errorMess = $this -getError();
return $return;
}
}
}
function copyFile(){//將文件從臨時目錄拷貝到目標文件夾
if(!$this-errorNum){
//如果傳遞來的路徑有斜杠,則刪除斜杠再加上斜杠
//./upload+./
$filepath=rtrim($this-filepath,’/’).’/’;
//./upload+./+加上隨機後的新文件名和後綴
//這裡指創建一個新的$filepath.這個文件 像佔位符但是為空的
$filepath.=$this-newFileName;
//嘗試着把臨時文件$this-tmpFileName移動到$filepath下哪裡覆蓋原來的這個文件
if(@move_uploaded_file($this-tmpFileName,$filepath)){
return true;
}else{
$this-setOption(‘errorNum’,-3);
return false;
}
}else{
return false;
}
}
//這裡是為了其他剩餘的屬性進行初始化操作!
private function setFiles($name=””,$tmp_name=””,$size=0,$error=0){
//這裡給錯誤號賦值
$this – setOption(“errorNum”,$error);
//如果這裡有錯誤,直接返回錯誤
if ($error){
return false;
}
$this – setOption(“originName”,$name);//複製名字為源文件名
$this – setOption(“tmpFileName”,$tmp_name);
$arrstr = explode(“.”,$name);//按點分割文件名,
//取分割後的字符串數組最後一個 並轉換為小寫,賦值為文件類型
$this – setOption(“fileType”,strtolower($arrstr[count($arrstr)-1]));
$this – setOption(“fileSize”,$size);
return true;
}
//用來獲取上傳後的文件名
function getNewFileName(){
return $this – newFileName;
}
//上傳失敗,後則返回這個方法,就可以產看報告
function getErrorMsg(){
return $this – errorMess;
}
}
?
============================調用====================================
?php
require(“FileUpload.class.php”);
//這裡實例化後賦值為數組,數組的下標要對應類中屬性的值,否則不能傳遞值,可以不分先後但是必須一致
$up = new FileUpload(array(‘israndname’=’true’,”filepath”=”./upload/”,’allowtype’=array(‘txt’,’doc’,’jpg’,’gif’),”maxsize”=1000000));
echo ‘pre’;
if($up – uploadFile(“pic”)){
print_r($up – getNewFileName());
} else{
print_r($up – getErrorMsg());
}
echo ‘pre’;
?
——————-HTML————————-
html
head
meta http-quive=”content-type” content=”text/html;charset=utf-8″ /meta
/head
body
form action=”upload.php” method=”post” enctype=”multipart/form-data”
shoppic:input type=”file” name=”pic[]”br
input type=”hidden” name=”MAX_FILE_SIZE” value=”1000000″
input type=”submit” name=”sub” value=”添加商品”
/form
/body
/html
——————-或者HTML————————-
html
head
meta http-quive=”content-type” content=”text/html;charset=utf-8″ /meta
/head
body
form action=”upload.php” method=”post” enctype=”multipart/form-data”
//區別在這裡
shoppic:input type=”file” name=”pic[]”br
shoppic:input type=”file” name=”pic[]”br
shoppic:input type=”file” name=”pic[]”br
input type=”hidden” name=”MAX_FILE_SIZE” value=”1000000″
input type=”submit” name=”sub” value=”添加商品”
/form
/body
/html
=====================================================================
以上是自己總結的 還沒有怎麼精簡加工過,僅供參考
以上不止可以上傳圖片,可以上自定義任何文件
php如何上傳圖片到數據庫
把圖片保存到服務器,拼接圖片地址
保存圖片地址到數據庫
讀取圖片地址就能訪問到圖片了。
原創文章,作者:RVWQ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/132971.html