文件上傳php資料庫源碼,php文件上傳到資料庫

本文目錄一覽:

網上下載的php源碼如何上傳安裝,求詳細教程!

源碼下載下來後,首先安裝php運行環境:簡單點的集成環境有wamp,xampp等,搜索下載安裝就可以了,具體安裝和配置方法搜百度wamp安裝使用方法。。

安裝好環境後,將源碼放入環境的相應項目目錄:如wamp默認在在www下,xampp是在htdoc;

如果是完整的CMS源碼,一般都只要訪問localhost就會有自動安裝引導,綁定資料庫;按提示操作就行,如果不是,那麼久要先綁定資料庫(nosql類型資料庫除外)選擇目錄中conf或者config,或者db,或者config.php等類似文件或者目錄配置你的資料庫或者緩存庫就可以測試了(如果每有編程基礎,建議先語言入門);操作簡單但還真得專人來

邊訪問,邊看錯,報錯改錯,知道沒錯,祝你早日入門

怎麼把php源碼資料庫導入資料庫

可以參考:

一般是單獨導入的,

在mysql上,要用mysql_import工具 把文本導入

sqlserver上可以用資料庫備份工具恢復導入, 也可以使用其他資料庫引擎通過ado到。

不需要源碼,但是需要了解php源碼所需要的庫表結構。一般php源碼里好多都有建立空庫結構的源碼。

怎麼上傳php網站源碼到自己的空間?

FTP軟體,,,然後上傳資料庫,在修改資料庫配置參數就行了。

至於傳到哪個文件夾內,根據空間商提供的不同名字的文件夾,這個不好確定,問問你的主機商吧。

直接傳文件不用解壓。

求php文件上傳源碼

?php

//文件和圖片上傳類

class UploadFile

{//類定義開始

public $maxSize = -1; // 上傳文件的最大值

public $supportMulti = true; // 是否支持多文件上傳

public $allowExts = array();// 允許上傳的文件後綴// 留空不作後綴檢 查

public $allowTypes = array(); // 允許上傳的文件類型 // 留空不做檢查

public $thumb = false; // 使用對上傳圖片進行縮略圖處理

public $thumbMaxWidth; // 縮略圖最大寬度

public $thumbMaxHeight; // 縮略圖最大高度

public $thumbPrefix = ‘thumb_’; // 縮略圖前綴

public $thumbSuffix = ”;

public $thumbPath = ”; // 縮略圖保存路徑

public $thumbFile = ”;// 縮略圖文件名

public $thumbRemoveOrigin =false;// 是否移除原圖

public $zipImages = false; // 壓縮圖片文件上傳

public $autoSub = false; // 啟用子目錄保存文件

public $subType = ‘hash’;// 子目錄創建方式 可以使用hash date

public $dateFormat = ‘Ymd’;

public $hashLevel = 1; // hash的目錄層次

public $savePath = ”; // 上傳文件保存路徑

public $autoCheck = true; // 是否自動檢查附件

public $uploadReplace = false;// 存在同名是否覆蓋

// 例如可以是 time uniqid com_create_guid 等

// 必須是一個無需任何參數的函數名 可以使用自定義函數

public $saveRule = ”;// 上傳文件命名規則

// 例如可以是 md5_file sha1_file 等// 上傳文件Hash規則函數名

public $hashType = ‘md5_file’;

private $error = ”;// 錯誤信息

private $uploadFileInfo ;// 上傳成功的文件信息

/**

+———————————————————-

* 架構函數

+———————————————————-

* @access public

+———————————————————-

*/

public function __construct($maxSize=”,$allowExts=”,$allowTypes=”,$savePath=”,$saveRule=”)

{

if(!empty($maxSize) is_numeric($maxSize)) {

$this-maxSize = $maxSize;

}

if(!empty($allowExts)) {

if(is_array($allowExts)) {

$this-allowExts = array_map(‘strtolower’,$allowExts);

}else {

$this-allowExts = explode(‘,’,strtolower($allowExts));

}

}

if(!empty($allowTypes)) {

if(is_array($allowTypes)) {

$this-allowTypes = array_map(‘strtolower’,$allowTypes);

}else {

$this-allowTypes = explode(‘,’,strtolower($allowTypes));

}

}

if(!empty($savePath)) {

$this-savePath = $savePath;

}

if(!empty($saveRule)) {

$this-saveRule = $saveRule;

}

}

private function save($file)

{

$filename = $file[‘savepath’].$file[‘savename’];

if(!$this-uploadReplace is_file($filename)) {// 不覆蓋同名文件

$this-error = ‘文件已經存在!’.$filename;

return $this – error;

}

// 如果是圖像文件 檢測文件格式

if( in_array(strtolower($file[‘extension’]),array(‘gif’,’jpg’,’jpeg’,’bmp’,’png’,’swf’)) $this – error === getimagesize($file[‘tmp_name’])) {

$this-error = ‘非法圖像文件’;

return $this – error;

}

if(!move_uploaded_file($file[‘tmp_name’], iconv(‘utf-8′,’gbk’,$filename))) {

$this-error = ‘文件上傳保存錯誤!’;

return $this – error;

}

if($this-thumb in_array(strtolower($file[‘extension’]),array(‘gif’,’jpg’,’jpeg’,’bmp’,’png’))) {

$image = getimagesize($filename);

if($this – error !== $image) {

//是圖像文件生成縮略圖

$thumbWidth = explode(‘,’,$this-thumbMaxWidth);

$thumbHeight = explode(‘,’,$this-thumbMaxHeight);

$thumbPrefix = explode(‘,’,$this-thumbPrefix);

$thumbSuffix = explode(‘,’,$this-thumbSuffix);

$thumbFile = explode(‘,’,$this-thumbFile);

$thumbPath =

$this-thumbPath?$this-thumbPath:$file[‘savepath’];

// 生成圖像縮略圖

if(file_exists(dirname(__FILE__).’/Image.class.php’))

{

require_once(dirname(__FILE__).’/Image.class.php’);

$realFilename = $this-autoSub?basename($file[‘savename’]):$file[‘savename’];

for($i=0,$len=count($thumbWidth); $i$len; $i++) {

$thumbname = $thumbPath.$thumbPrefix[$i].substr($realFilename,0,strrpos($realFilename, ‘.’)).$thumbSuffix[$i].’.’.$file[‘extension’];

Image::thumb($filename,$thumbname,”,$thumbWidth[$i],$thumbHeight[$i],true);

}

if($this-thumbRemoveOrigin) {

// 生成縮略圖之後刪除原圖

unlink($filename);

}

}

}

}

return true;

}

/**

+———————————————————-

* 上傳文件

+———————————————————-

* @access public

+———————————————————-

* @param string $savePath 上傳文件保存路徑

+———————————————————-

* @return string

+———————————————————-

* @throws ThinkExecption

+———————————————————-

*/

public function upload($savePath =”) {

if(empty($savePath)) //如果不指定保存文件名,則由系統默認

$savePath = $this-savePath;

$savePath .= date(‘Ym’,time()).”/”;

if(!is_dir($savePath)) { // 檢查上傳目錄

if(is_dir(base64_decode($savePath))) {// 檢查目錄是否編碼後的

$savePath = base64_decode($savePath);

}else{

if(!mkdir($savePath)){ // 嘗試創建目錄

$this-error = ‘上傳目錄’.$savePath.’不存在’;return $this – error;

}

}

}else {

if(!is_writeable($savePath)) {

$this-error = ‘上傳目錄’.$savePath.’不可寫’; return $this – error;

}

}

$fileInfo = array();

$isUpload = $this – error;

// 獲取上傳的文件信息

// 對$_FILES數組信息處理

$files = $this-dealFiles($_FILES);

foreach($files as $key = $file) {

//過濾無效的上傳

if(!empty($file[‘name’])) {

//登記上傳文件的擴展信息

$file[‘key’] = $key;

$file[‘extension’] = $this-getExt($file[‘name’]);

$file[‘savepath’] = $savePath;

$file[‘savename’] = $this-getSaveName($file);

// 自動檢查附件

if($this-autoCheck) {

if(!$this-check($file))

return $this – error;

}

//保存上傳文件

//echo “pre”;print_r( $file );

if(!$this-save($file)) return $this – error;

/*

if(function_exists($this-hashType)) {

$fun = $this-hashType;

$file[‘hash’] = $fun(auto_charset($file[‘savepath’].$file[‘savename’],’utf-8′,’gbk’));

}

*/

//上傳成功後保存文件信息,供其他地方調用

unset($file[‘tmp_name’],$file[‘error’]);

$fileInfo[] = $file;

$isUpload = true;

}

}

if($isUpload) {

$this-uploadFileInfo = $fileInfo;

return $fileInfo;

}else {

$this-error = ‘沒有選擇上傳文件’;

return $this – error;

}

}

/**

+———————————————————-

* 轉換上傳文件數組變數為正確的方式

+———————————————————-

* @access private

+———————————————————-

* @param array $files 上傳的文件變數

+———————————————————-

* @return array

+———————————————————-

*/

private function dealFiles($files) {

$fileArray = array();

foreach ($files as $file){

if(is_array($file[‘name’])) {

$keys = array_keys($file);

$count = count($file[‘name’]);

for ($i=0; $i$count; $i++) {

foreach ($keys as $key)

$fileArray[$i][$key] = $file[$key][$i];

}

}else{

$fileArray = $files;

}

break;

}

return $fileArray;

}

/**

+———————————————————-

* 獲取錯誤代碼信息

+———————————————————-

* @access public

+———————————————————-

* @param string $errorNo 錯誤號碼

+———————————————————-

* @return void

+———————————————————-

* @throws ThinkExecption

+———————————————————-

*/

protected function error($errorNo)

{

switch($errorNo) {

case 1:

$this-error = ‘上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值’;

break;

case 2:

$this-error = ‘上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值’;

break;

case 3:

$this-error = ‘文件只有部分被上傳’;

break;

case 4:

$this-error = ‘沒有文件被上傳’;

break;

case 6:

$this-error = ‘找不到臨時文件夾’;

break;

case 7:

$this-error = ‘文件寫入失敗’;

break;

default:

$this-error = ‘未知上傳錯誤!’;

}

return ;

}

/**

+———————————————————-

* 根據上傳文件命名規則取得保存文件名

+———————————————————-

* @access private

+———————————————————-

* @param string $filename 數據

+———————————————————-

* @return string

+———————————————————-

*/

private function getSaveName($filename)

{

$rule = $this-saveRule;

if(empty($rule)) {//沒有定義命名規則,則保持文件名不變

$saveName = $filename[‘name’];

}else {

if(function_exists($rule)) {

//使用函數生成一個唯一文件標識號

$saveName = $rule().rand(1001,9999).”.”.$filename[‘extension’];

}else {

//使用給定的文件名作為標識號

$saveName = $rule.rand(1001,9999).”.”.$filename[‘extension’];

}

}

if($this-autoSub) {

// 使用子目錄保存文件

$saveName = $this-getSubName($filename).’/’.$saveName;

}

return $saveName;

}

/**

+———————————————————-

* 獲取子目錄的名稱

+———————————————————-

* @access private

+———————————————————-

* @param array $file 上傳的文件信息

+———————————————————-

* @return string

+———————————————————-

*/

private function getSubName($file)

{

switch($this-subType) {

case ‘date’:

$dir = date($this-dateFormat,time());

break;

case ‘hash’:

default:

$name = md5($file[‘savename’]);

$dir = ”;

for($i=0;$i$this-hashLevel;$i++) {

$dir .= $name{0}.’/’;

}

break;

}

if(!is_dir($file[‘savepath’].$dir)) {

mkdir($file[‘savepath’].$dir);

}

return $dir;

}

/**

+———————————————————-

* 檢查上傳的文件

+———————————————————-

* @access private

+———————————————————-

* @param array $file 文件信息

+———————————————————-

* @return boolean

+———————————————————-

*/

private function check($file) {

if($file[‘error’]!== 0) {

//文件上傳失敗

//捕獲錯誤代碼

$this-error($file[‘error’]);

return $this – error;

}

//檢查文件Mime類型

if(!$this-checkType($file[‘type’])) {

$this-error = ‘上傳文件MIME類型不允許!’;

return $this – error;

}

//檢查文件類型

if(!$this-checkExt($file[‘extension’])) {

$this-error =’上傳文件類型不允許’;

return $this – error;

}

//文件上傳成功,進行自定義規則檢查

//檢查文件大小

if(!$this-checkSize($file[‘size’])) {

$this-error = ‘上傳文件大小超出限制!’;

return $this – error;

}

//檢查是否合法上傳

if(!$this-checkUpload($file[‘tmp_name’])) {

$this-error = ‘非法上傳文件!’;

return $this – error;

}

return true;

}

/**

+———————————————————-

* 檢查上傳的文件類型是否合法

+———————————————————-

* @access private

+———————————————————-

* @param string $type 數據

+———————————————————-

* @return boolean

+———————————————————-

*/

private function checkType($type)

{

if(!empty($this-allowTypes))

return in_array(strtolower($type),$this-allowTypes);

return true;

}

/**

+———————————————————-

* 檢查上傳的文件後綴是否合法

+———————————————————-

* @access private

+———————————————————-

* @param string $ext 後綴名

+———————————————————-

* @return boolean

+———————————————————-

*/

private function checkExt($ext)

{

if(!empty($this-allowExts))

return in_array(strtolower($ext),$this-allowExts,true);

return true;

}

/**

+———————————————————-

* 檢查文件大小是否合法

+———————————————————-

* @access private

+———————————————————-

* @param integer $size 數據

+———————————————————-

* @return boolean

+———————————————————-

*/

private function checkSize($size)

{

return !($size $this-maxSize) || (-1 == $this-maxSize);

}

/**

+———————————————————-

* 檢查文件是否非法提交

+———————————————————-

* @access private

+———————————————————-

* @param string $filename 文件名

+———————————————————-

* @return boolean

+———————————————————-

*/

private function checkUpload($filename)

{

return is_uploaded_file($filename);

}

/**

+———————————————————-

* 取得上傳文件的後綴

+———————————————————-

* @access private

+———————————————————-

* @param string $filename 文件名

+———————————————————-

* @return boolean

+———————————————————-

*/

private function getExt($filename)

{

$pathinfo = pathinfo($filename);

return $pathinfo[‘extension’];

}

/**

+———————————————————-

* 取得上傳文件的信息

+———————————————————-

* @access public

+———————————————————-

* @return array

+———————————————————-

*/

public function getUploadFileInfo()

{

return $this-uploadFileInfo;

}

/**

+———————————————————-

* 取得最後一次錯誤信息

+———————————————————-

* @access public

+———————————————————-

* @return string

+———————————————————-

*/

public function getErrorMsg()

{

return $this-error;

}

}//類定義結束

?

php多文件上傳實現代碼

這篇文章主要介紹了php多文件上傳實現代碼,需要的朋友可以參考下

index_uploads.php

代碼如下:

html

head

meta

charset=”utf-8″

titleindex_uploads/title

/head

body

form

action=”uploads.php”

method=”post”

enctype=”multipart/form-data”

input

type=”file”

name=”file[]”

br

input

type=”file”

name=”file[]”

br

input

type=”submit”

value=”uploads”

/form

/body

/html

uploads.php

代碼如下:

?php

header(“content-type:text/html;charset=utf-8”);

echo

“pre”;

print_r($_FILES);

echo

“/pre”;

$count

=

count($_FILES[‘file’][‘name’]);

for

($i

=

0;

$i

$count;

$i++)

{

$tmpfile

=

$_FILES[‘file’][‘tmp_name’][$i];

$filefix

=

array_pop(explode(“.”,

$_FILES[‘file’][‘name’][$i]));

$dstfile

=

“uploads/files/”.time().”_”.mt_rand().”.”.$filefix;

if

(move_uploaded_file($tmpfile,

$dstfile))

{

echo

“scriptalert(‘succeed!’);window.location.href=’index_uploads.php’;/script”;

}

else

{

echo

“scriptalert(‘fail!’);window.location.href=’index_uploads.php’;/script”;

}

}

核心:1上傳首頁中input的name屬性是這麼設置的。

 2用while循環上傳多文件。

php 文件上傳源碼

簡單 上傳什麼都可以

upload.html:

body

form enctype=”multipart/form-data” action=”upload.php” method=”POST”

input name=”userfile1″ type=”file” /br /

input type=”submit” value=”上傳” /

/form

/body

upload.php:

?

$uploaddir = ‘uploads/’; //上傳的目錄

if(!is_dir($uploaddir)){mkdir($uploaddir,0777); }

$uploadfile1 = $uploaddir.$_FILES[‘userfile1’][‘name’];

if($_FILES[‘userfile1’][‘name’]!=””){

move_uploaded_file($_FILES[‘userfile1’][‘tmp_name’], $uploadfile1);

}

?

不明白問我好了

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/185860.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-26 21:08
下一篇 2024-11-26 21:08

相關推薦

  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • Python 常用資料庫有哪些?

    在Python編程中,資料庫是不可或缺的一部分。隨著互聯網應用的不斷擴大,處理海量數據已成為一種趨勢。Python有許多成熟的資料庫管理系統,接下來我們將從多個方面介紹Python…

    編程 2025-04-29
  • openeuler安裝資料庫方案

    本文將介紹在openeuler操作系統中安裝資料庫的方案,並提供代碼示例。 一、安裝MariaDB 下面介紹如何在openeuler中安裝MariaDB。 1、更新軟體源 sudo…

    編程 2025-04-29
  • 雲智直聘 源碼分析

    本文將會對雲智直聘的源碼進行分析,包括前端頁面和後端代碼,幫助讀者了解其架構、技術實現以及對一些常見的問題進行解決。通過本文的閱讀,讀者將會了解到雲智直聘的特點、優勢以及不足之處,…

    編程 2025-04-29
  • PHP怎麼接幣

    想要在自己的網站或應用中接受比特幣等加密貨幣的支付,就需要對該加密貨幣擁有一定的了解,並使用對應的API進行開發。本文將從多個方面詳細闡述如何使用PHP接受加密貨幣的支付。 一、環…

    編程 2025-04-29
  • 資料庫第三範式會有刪除插入異常

    如果沒有正確設計資料庫,第三範式可能導致刪除和插入異常。以下是詳細解釋: 一、什麼是第三範式和範式理論? 範式理論是關係資料庫中的一個規範化過程。第三範式是範式理論中的一種常見形式…

    編程 2025-04-29
  • leveldb和unqlite:兩個高性能的資料庫存儲引擎

    本文將介紹兩款高性能的資料庫存儲引擎:leveldb和unqlite,並從多個方面對它們進行詳細的闡述。 一、leveldb:輕量級的鍵值存儲引擎 1、leveldb概述: lev…

    編程 2025-04-28
  • Python怎麼導入資料庫

    Python是一種高級編程語言。它具有簡單、易讀的語法和廣泛的庫,讓它成為一個靈活和強大的工具。Python的資料庫連接類型可以多種多樣,其中包括MySQL、Oracle、Post…

    編程 2025-04-28
  • 使用PHP foreach遍歷有相同屬性的值

    本篇文章將介紹如何使用PHP foreach遍歷具有相同屬性的值,並給出相應的代碼示例。 一、基礎概念 在講解如何使用PHP foreach遍歷有相同屬性的值之前,我們需要先了解幾…

    編程 2025-04-28
  • Python網站源碼解析

    本文將從多個方面對Python網站源碼進行詳細解析,包括搭建網站、數據處理、安全性等內容。 一、搭建網站 Python是一種高級編程語言,適用於多種領域。它也可以用於搭建網站。最常…

    編程 2025-04-28

發表回復

登錄後才能評論