導航:首頁 > 編程語言 > phpfileuploads

phpfileuploads

發布時間:2022-08-22 13:10:30

php 使用ajaxfileupload無法接收文件

既然你都在php頁面列印$_FILE沒有值說明沒有數據上傳到php文件。


ajax上傳文件:

$(document).ready(function(){
$('#test').click(function(){
$.ajaxFileUpload({
url:'接收路徑',
secureuri:false,
fileElementId:'file',
dataType:'text',//返回數據類型
success:function(data,status){
//alert(data);
$("#img").attr("src","圖片地址
);
},
error:function(data,status,e)//伺服器響應失敗處理函數
{
alert(e);
}
});
//$('#upload').submit();
});
});

❷ php上傳程序中upload.php文件是什麼意思

那是處理上傳文件的後台php文件。~~取決於你php程序的寫法。php類的網站本來就多個文件協同工作的。你要把上傳的處理文件寫進你其他的php文件里也可以。取決於你php程序的結構。一般的php軟體的開發員,都喜歡把某些功能,比如上傳功能,專門拿出來寫在一個單獨的php里。這樣結構才清晰,方便管理和以後修改程序。

❸ php怎麼一個file上傳多張圖片

<?php /** *類說明: * 使用new關鍵字實像化類,類中有兩個公用方法, * 方法create_input創建表單按鍵,請在相應的表單處引用該方法就可創建上傳表單的input按鍵 * 方法get_upfile()用於處理上傳文件 * 該類由 游天小蝦 製作,網頁製作交流群:69574955 * **/ class upfile { private $name = 'filename';//input表單名 private $namecount = 2;//設置上傳文件的個數 private $type = array('jpg','jpeg','gif','png');//文件格式 private $size = '1024';//文件大小單位kb private $upname = '';//上傳文件信息 private $updir = 'upfile/'; private $movename = '';//移動後的文件名 private $uparrs = array();//多文件上傳數組 private $error_type =0;//文件上傳產生的錯誤 /** * 創建文件上傳的表單控制項 * */ public function create_input(){ if(floor($this->namecount) == 1){ $input = "<p><input type='file' id=".$this->name." name=".$this->name."></p>"; }else{ for($i=0;$i<($this->namecount);$i++){ $input .= "<p><input type='file' id='".$this->name."[]' name='".$this->name."[]'></p>"; } } echo "$input"; } /** * 初始文件信息$file = $_FILES['file']['tem_name'] * **/ private function get_part(){ if($this->namecount == 1){ //判斷是否是多文件上傳 if($_FILES[$this->name]['tmp_name']){ $this->upname = $_FILES[$this->name]; }else{ $this->error_type += 100; //文件信息錯誤觀點 100; } }else{ if($_FILES[$this->name]){ $this->uparrs = $this->more_updata($_FILES[$this->name],$this->namecount);//對$_FILES取得的文件上信息重寫 }else{ $this->error_type += 100; //文件信息錯誤觀點 100; } } } /** * 多文件上傳時,數組重寫 * **/ private function more_updata($arrs,$num){ for($i=0;$i<$num;$i++){ $data[] =array('name'=>$arrs[name][$i],'type'=>$arrs[type][$i],'tmp_name'=>$arrs[tmp_name][$i],'error'=>$arrs['error'][$i],'size'=>$arrs['size'][$i]); } return $data; } /** * 判斷上傳文件大小 * **/ private function chck_size(){ if($this->upname['size']*1000 < $this->size){ $this->error_type += 300; //文件信息錯誤觀點 300; } } /** * 判斷上傳文件的類型 * **/ private function chck_type(){ if(!in_array($this->get_suffix($this->upname['name']),$this->type)){ $this->error_type += 500; //文件信息錯誤觀點 500; } } /** * 格式化上傳後的文件名 * **/ private function chck_name(){ $this->movename = date(Ymd).substr(md5(rand(0,date(Hms))),0,6)."."; $this->movename .= $this->get_suffix($this->upname['name']); } /** * 移動文件 * **/ private function move_file(){ if($this->updir){ if(!move_uploaded_file($this->upname['tmp_name'],$this->updir.$this->movename)){ $this->error_type += 700; //文件信息錯誤觀點 700; } }else{ mkdir($this->updir,"w"); chmod($this->updir,777); if(!move_uploaded_file($this->upname['tmp_name'],$this->updir.$this->movename)){ $this->error_type += 700; //文件信息錯誤觀點 700; } } } /** * 取得文件的後綴名 * **/ private function get_suffix($filename){//取得文件後綴名 $part = pathinfo($filename); $suffix = $part['extension']; return $suffix; } /** * 文件上傳處理 * **/ public function get_upfile() {//主上傳方法 if(floor($this->namecount) == 1){ $this->get_part(); $this->chck_name(); $this->chck_type(); $this->chck_size(); if($this->error_type ==0){$this->move_file();} if($this->error_type ==0){ echo "$this->movename 上傳成功 <br>"; }else{ echo "$this->movename 上傳失敗,錯誤: $this->error_type <br>"; $this->error_type=0; }; }else{ $this->get_part(); for($i=0;$i<floor($this->namecount);$i++){ $this->upname = ($this->uparrs[$i]); $this->chck_name(); $this->chck_type(); $this->chck_size(); if($this->error_type ==0){$this->move_file();} if($this->error_type ==0){ echo "$this->movename 上傳成功 <br>"; }else{ echo "$this->movename 上傳失敗,錯誤: $this->error_type <br>"; $this->error_type=0; }; } } } } $up = new upfile(); if($_POST['t1']){ $up->get_upfile(); } ?> <form name='f1' enctype = multipart/form-data action="" method="post"> <input type='text' name='t1'><br> <?php $up->create_input(); ?> <input type='submit' value='上傳'> </from> 剛不久寫的一個文件上傳的類!上面已經有說明了,你參考一下,不明白的話,可以問我,或者加入我們的QQ群討論!

❹ php 圖片上傳move_uploaded_file 出錯

上傳文件不存在。很可能是PHP臨時文件夾不存在導致的。

<?php
/*
* 文件上傳類
*/
class Uploads{

//上傳文件
private $uploadFile;

//上傳文件擴展名
private $ext = array('jpeg','jpg','gif','png');

//上傳文件大小
private $size = 5000000;

//上傳文件目錄
private $uploadDir = './uploads/';

//是否自定義名稱,默認FALSE
private $newName = '';

//上傳文件是否可讀,默認為TRUE
private $isRead = TRUE;

//上傳文件是否可寫,默認為TRUE
private $isWrite = TRUE;

//上傳信息
private $info;

/*
* 文件上傳類初始化
*/
public function __construct($newName='',$ext='',$size='',$dir='',$isRead=TRUE,$isWrite=TRUE){
$this->ext = empty($ext)?$this->ext:$ext;
$this->size = empty($size)?$this->size:$size;
$this->dir = empty($dir)?$this->uploadDir:$dir;
$this->newName = $newName;
$this->isRead = $isRead?TRUE:FALSE;
$this->isWrite = $isWrite?TRUE:FALSE;
}
/*
* 處理上傳文件
*/
public function doUpload(){
$this->checkData();
$this->checkFile() or $this->error();
$this->checkExt() or $this->error();
$this->checkSize() or $this->error();
$this->checkError() or $this->error();
$this->checkDir() or $this->error();
$this->upload() or $this->error();
return $this->info['msg'];
}
/*
* 處理上傳文件數據
*/
public function checkData(){
$num = 0;
$newArr = array();
foreach($_FILES as $v){
if(is_array($v['name'])){
$count = count($v['name']);
for($i=0; $i<$count; $i++){
foreach($v as $m=>$n){
$newArr[$num][$m] = $n[$i];
}
$num++;
}
}else{
$newArr[$num] = $v;
$num++;
}

}
$endArr = array();
foreach($newArr as $v){
if($v['name'] != ''){
$endArr[]=$v;
}
}
$this->uploadFile = $endArr;
}
/*
* 檢測上傳文件是否存在
*/
private function checkFile(){
if(empty($this->uploadFile)){
$this->info['error'] = '上傳文件不得為空!!!';
return FALSE;
}
return TRUE;
}
/*
* 檢測上傳文件類型是否合法
*/

private function checkExt(){
if(!is_array($this->ext)){
$this->ext = explode(',', $this->ext);
}
foreach($this->uploadFile as $v){
$ext = strtolower(substr(strrchr(basename($v['name']),'.'),1));
if(!in_array($ext,$this->ext)){
$this->info['error'] = '上傳文件類型非法,禁止上傳!!!';
return FALSE;
}
}
return TRUE;
}

/*
* 檢測上傳文件大小
*/

private function checkSize(){
foreach($this->uploadFile as $v){
if($v['size']>$this->size){
$this->info['error'] = '上傳文件體積過大,上傳失敗!!!';
return FALSE;
}
}
return TRUE;
}

/*
* 檢測文件上傳錯誤代碼
*/
private function checkError(){
foreach($this->uploadFile as $v){
switch($v['error']){
case 0:
return TRUE;
break;
case 1:
$this->info['error'] = '上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值,上傳失敗!!!';
return FALSE;
break;
case 2:
$this->info['error'] = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值,上傳失敗!!!';
return FALSE;
break;
case 3:
$this->info['error'] = '文件只有部分被上傳!!!';
return FALSE;
break;
case 4:
$this->info['error'] = '沒有文件上傳!!!';
return FALSE;
break;
}
}
return TRUE;
}

/*
* 檢測上傳文件夾是否存在
*/

private function checkDir(){
if(!file_exists($this->uploadDir)){
mkdir($this->uploadDir,0777,true);
}
if(!is_writeable($this->uploadDir)){
$this->info['error'] = '上傳目錄沒有寫入許可權,上傳失敗!!!';
return FALSE;
}
return TRUE;
}

/*
* 上傳文件
*/

private function upload(){

date_default_timezone_set('PRC');

//檢測文件是否自定義名稱
$name = empty($this->newName)?date('Ymd_His'):$this->newName;
foreach($this->uploadFile as $k=>$v){
$upload_path = $this->uploadDir.$name.'_'.($k+1).strrchr(basename($v['name']),'.');
$upload_path = iconv('UTF-8','gbk',$upload_path);
if(is_uploaded_file($v['tmp_name'])){
if(move_uploaded_file($v['tmp_name'], $upload_path)){
if($this->isRead && $this->isWrite){
chmod($upload_path,0777);
}else if($this->isRead && !$this->isWrite){
chmod($upload_path,0444);
}else if(!$this->isRead && $this->isWrite){
chmod($upload_path,0222);
}else{
chmod($upload_path,0000);
}
$this->info['msg']=array('type'=>1,'success'=>'文件上傳成功','path'=>iconv('gbk','UTF-8',$upload_path));
}else{
$this->info['error'] = '文件上傳失敗!!!';
return FALSE;
}
}
}
return TRUE;

}
/*
* 上傳成功的方法
*/
public function success(){
echo $this->info['msg']['success'];
}

/*
* 上傳文件錯誤方法
*/
public function error(){
echo $this->info['error'];
die;
}

這是我寫的PHP類,你可以參考一下。有什麼特殊需要的,你可以告訴我一下,完善一下上傳類。

❺ php 編寫 實現上傳圖片至伺服器的函數

<?php
classFileUpload{
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($optionsas$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);
}
}
privatefunctionsetOption($key,$val){
//讓實例化後獲取過來的數組下標=數組下標的值,這里即為構造函數初始化
//構造函數中調用,等於把所有屬性初始化,將來可以直接訪問
$this->$key=$val;
}

privatefunctiongetError(){
$str="上傳文件{$this->originName}時出錯";
switch($this->errorNum){
case4:$str.="沒有文件被上傳";
break;
case3:$str.="文件只有部分上傳";
break;
case2:$str.="上傳文件超過了表單的值";
break;
case1:$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>';
}
//用來檢查文件上傳路徑
privatefunctioncheckFilePath(){
if(empty($this->filepath)){
$this->setOption("errorNum",-5);
returnfalse;
}
if(!file_exists($this->filepath)||!is_writable($this->filepath)){
if(!@mkdir($this->filepath,0755)){
$this->setOption("errorNum",-4);
returnfalse;
}
}
returntrue;
}
//用來檢查上傳文件尺寸大小

privatefunctioncheckFileSize(){
if($this->fileSize>$this->maxsize){
$this->setOption("errorNum",-2);
returnfalse;
}else{
returntrue;
}
}

//用來檢查文件上傳類型
privatefunctioncheckFileType(){
if(in_array(strtolower($this->fileType),$this->allowtype)){
returntrue;
}else{
//如果$this->fileType這個類型不在$this->allowtype這個數組中,則把錯誤號變成-1
$this->setOption("errorNum",-1);
returnfalse;
}
}
privatefunctionsetNewFileName(){
if($this->israndname){
$this->setOption("newFileName",$this->preRandName());
}else{
$this->setOption("newFileName",$this->originName);
}
}
//用於檢查文件隨機文件名
privatefunctionpreRandName(){
$fileName=date("Ymdhis").rand(100,999);
return$fileName.".".$this->fileType;
}
//用來上傳一個文件

functionuploadFile($fileField){
//檢查文件路徑
$return=true;
if(!$this->checkFilePath()){
$this->errorMess=$this->getError();
returnfalse;
}//獲取文件信息
$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;$i<count($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;$i<count($name);$i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
$this->setNewFileName();
if(!$this->File()){
$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->File()){
returntrue;
}else{
returnfalse;
}
}else{
$return=false;
}
}else{
$return=false;
}
if(!$return){
$this->errorMess=$this->getError();
return$return;
}
}

}

functionFile(){//將文件從臨時目錄拷貝到目標文件夾
if(!$this->errorNum){
//如果傳遞來的路徑有斜杠,則刪除斜杠再加上斜杠
//./upload+./
$filepath=rtrim($this->filepath,'/').'/';
//./upload+./+加上隨機後的新文件名和後綴
//這里指創建一個新的$filepath.這個文件像佔位符但是為空的
$filepath.=$this->newFileName;
//嘗試著把臨時文件$this->tmpFileName移動到$filepath下哪裡覆蓋原來的這個文件
if(@move_uploaded_file($this->tmpFileName,$filepath)){
returntrue;
}else{
$this->setOption('errorNum',-3);
returnfalse;
}
}else{
returnfalse;
}
}
//這里是為了其他剩餘的屬性進行初始化操作!
privatefunctionsetFiles($name="",$tmp_name="",$size=0,$error=0){
//這里給錯誤號賦值
$this->setOption("errorNum",$error);
//如果這里有錯誤,直接返回錯誤
if($error){
returnfalse;
}
$this->setOption("originName",$name);//復制名字為源文件名
$this->setOption("tmpFileName",$tmp_name);
$arrstr=explode(".",$name);//按點分割文件名,
//取分割後的字元串數組最後一個並轉換為小寫,賦值為文件類型
$this->setOption("fileType",strtolower($arrstr[count($arrstr)-1]));
$this->setOption("fileSize",$size);
returntrue;
}
//用來獲取上傳後的文件名
functiongetNewFileName(){
return$this->newFileName;
}

//上傳失敗,後則返回這個方法,就可以產看報告
functiongetErrorMsg(){
return$this->errorMess;
}
}
?>


============================調用====================================


<?php
require("FileUpload.class.php");
//這里實例化後賦值為數組,數組的下標要對應類中屬性的值,否則不能傳遞值,可以不分先後但是必須一致
$up=newFileUpload(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>
<metahttp-quive="content-type"content="text/html;charset=utf-8"></meta>
</head>
<body>
<formaction="upload.php"method="post"enctype="multipart/form-data">
shoppic:<inputtype="file"name="pic[]"><br>

<inputtype="hidden"name="MAX_FILE_SIZE"value="1000000">
<inputtype="submit"name="sub"value="添加商品">
</form>
</body>

</html>

-------------------或者HTML-------------------------

<html>
<head>
<metahttp-quive="content-type"content="text/html;charset=utf-8"></meta>
</head>
<body>
<formaction="upload.php"method="post"enctype="multipart/form-data">
//區別在這里
shoppic:<inputtype="file"name="pic[]"><br>
shoppic:<inputtype="file"name="pic[]"><br>
shoppic:<inputtype="file"name="pic[]"><br>
<inputtype="hidden"name="MAX_FILE_SIZE"value="1000000">
<inputtype="submit"name="sub"value="添加商品">
</form>
</body>

</html>
=====================================================================



以上是自己總結的 還沒有怎麼精簡加工過,僅供參考

以上不止可以上傳圖片,可以上自定義任何文件

❻ windows下怎麼安裝php7,php7裡面沒有這個php7apache2

第一步:安裝Apache服務。

在這里主要配置Network Domain、Server Name、Email地址以及Apache服務的佔用埠,默認為80埠,你可以根據需要配置在其他埠,Apache的安裝目錄你可以使用默認目錄或根據需要自行選擇安裝目錄。
在完成apache服務的安裝後,在游覽器中輸入http://localhost/,出現It』s work!字樣,說明Apache服務安裝成功了。

第二步:MySQL的安裝
選擇Custom自定義安裝

點擊「Change」更改你需要的目錄

待文件復制安裝完畢後,進行MySQL設置界面,單擊Finish。

選擇Detailed Configuration進行詳細配置

選擇MySQL運行模式:Server Machine

選擇MySQL資料庫默認存儲方式:Non-Trans Only (MYISAM)

設定MySQL最大連接數:一般設置為128 – 512之間的整數。

設定MySQL網路參數,注意:不要勾選啟用Strict Mode!那個Addfirewall的選項僅在需要外連MySQL的時候勾選上,也就是說給防火牆加個出入站的策略。

設定MySQL默認字元集:以用戶站點語言為准,默認我們填gbk。

Windows環境設定

修改設定root用戶密碼,root密碼請設置的比較復雜些,如果你出現的是三行,第一行的current password不要填寫,留空白

注意:非必要情況下,不要啟動遠程連接模式! 完成MySQL的安裝,並啟動MySQL服務。

完成後,打開cmd命令提示符窗口,輸入命令:mysql –u root –p

輸入剛才安裝時設置的密碼後,如果能夠正常進入MySQL控制台則說明MySQL安裝正常。

更改MySQL資料庫存放目錄(可選,默認是在C:ProgramDataMySQLMySQL Server 5.1data,隱藏目錄)

打開cmd命令提示符窗口,輸入命令:net stop mysql 停止MySQL服務運行;

打開你的mysql安裝目錄的my.ini,

找到:
Datadir = 「XXXXdata」

修改為:
Datadir = 「你需要的mysql安裝目錄,如:F:Database」

將C:ProgramDataMySQLMySQL Server 5.1data 文件夾復制到你修改的Datadir目錄,注意不是作為子目錄,如果你的Datadir寫的是F:Database,那麼你就把C:ProgramDataMySQLMySQL Server 5.1data復制到F盤,然後改名為Database;
打開cmd命令提示符窗口,
輸入命令:net start mysql

啟動MySQL服務。

重新測試MySQL是否工作正常。
第三步:安裝PHP。
其實在Windows 7下進行PHP安裝非常簡單,由於我下的是PHP代碼包,只要解壓php-5.3.2-Win32-VC6-x86並重名為文件夾為php,將其復制到C盤目錄下即可完成PHP安裝。
PHP環境搭建第三步:進行PHP配置環境。
壓縮下載的php-5.2.8-Win32.zip文件到你指定的目錄
然後修改php.ini

進入PHP文件夾,將php.ini-dist 重命名為php.ini ;
打開php.ini文件,找到:
extension_dir= 「./」

將其改為:
extension_dir= 「你的php解壓目錄ext」

找到:Windows Extensions

在Windows Extensions下方的動態模塊配置中,需要打開以下模塊支持:(去掉模塊配置每行前面的分號即可)
php_curl.dll
php_pdo_mysql.dll
php_gd2.dll
php_mbstring.dll
php_mcrypt.dll
php_mhash.dll
php_ming.dll
php_mysql.dll
php_openssl.dll
php_sockets.dll
php_xmlrpc.dll
php_zip.dll

找到:
disable_functions=
改為:
disable_functions=passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
保存php.ini文件,並將其復制到Y:Windows,然後把php文件夾下的libmysql.dll復制到C:WindowsSystem32目錄下。
接著配置PHP的Session功能(可選)
在使用session功能時,我們必須配置session文件在伺服器上的保存目錄,否則無法使用session,我們需要在Windows 7上新建一個可讀寫的目錄文件夾,此目錄最好獨立於WEB主程序目錄之外,此處我在D盤根目錄上建立了phpsessiontmp目錄,然後在php.ini配置文件中找到
;session.save_path = 「/tmp」
修改為
session.save_path = 「D:/phpsessiontmp」
配置PHP的文件上傳功能 (可選)
同session一樣,在使用PHP文件上傳功能時,我們必須要指定一個臨時文件夾以完成文件上傳功能,否則文件上傳功能會失敗,我們仍然需要在Windows 7上建立一個可讀寫的目錄文件夾,此處我在D盤根目錄上建立了phpfileuploadtmp目錄,然後在php.ini配置文件中找到
;upload_tmp_dir =
修改為
upload_tmp_dir = 「D:/phpfileuploadtmp」
第五步:配置Apache以支持PHP
1、打開你的apache2的安裝目錄,找到conf文件,打開裡面的httpd.conf
在#LoadMole vhost_alias_mole moles/mod_vhost_alias.so下添加

復制代碼
代碼如下:

LoadMole php5_mole "你的php安裝目錄/php5apache2_2.dll"
PHPIniDir "c:/Windows" (因為把php.ini復制到了C:/Windows目錄中了)
AddType application/x-httpd-php .php .html .htm

我們在PHP目錄下可以看到多個php5apache的DLL文件,由於我們使用的是Apache2.2.15,所以我們當然需要使用php5apache2_2.dll,接著指定PHP的安裝目錄以及執行的程序擴展名。
2、我們應該知道默認Apache伺服器執行WEB主程序的目錄為Apache2.2/htdocs,所以當你的WEB主程序目錄變更時,我們需要修改相應的Apache配置,即將

復制代碼
代碼如下:

DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"

修改為

復制代碼
代碼如下:

DocumentRoot "D:/PHPWeb(目錄自己定,我這是隨便寫的)"

還有

復制代碼
代碼如下:

<Directory "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs">

修改為

<Directory "D:/PHPWeb(目錄自己定,我這是隨便寫的)">
最後修改具體的index文件先後順序,由於配置了PHP功能,當然需要index.php優先執行

DirectoryIndex index.html
修改為

DirectoryIndex index.php index.html
最後重啟Apache伺服器

在一切工作完成後,在你剛才指定的工作目錄中(D:/PHPWeb)新建php文件,輸入以下內容

復制代碼
代碼如下:

<?
phpinfo();
?>

保存,在瀏覽器中輸入http://localhost/剛才新建的php文件名.php,如果出現php信息的話,就說明成功了。然後是資料庫的鏈接,把剛才建的php文件的內容替換為

復制代碼

❼ php文件上傳(利用ajaxfileupload.js)

這個是js錯誤,是ajax無法解析返回結果導致的錯誤, $.ajaxFileUpload 的返回值支持 xml 和 json格式

如果dataType 設置為json 格式 在php 文件要輸出的話 就需要用echo json_encode($_FILES); 來輸出$_FILES 數組中所有的值,
如果只需要輸入部分 就需要構造個 這樣:
$res = array();
$res['file_name'] = $_FILES['file']['name'];
echo json_encode($res);

如果dataType 設置為 xml 的話,那就要自己將輸出結果構造成 xml格式

❽ 求php文件上傳類,最好有詳細的代碼注釋,非常感謝!

<?php
/**
file: fileupload.class.php 文件上傳類FileUpload
本類的實例對象用於處理上傳文件,可以上傳一個文件,也可同時處理多個文件上傳
*/
class FileUpload {
private $path = "./uploads"; //上傳文件保存的路徑
private $allowtype = array('jpg','gif','png'); //設置限制上傳文件的類型
private $maxsize = 1000000; //限制文件上傳大小(位元組)
private $israndname = true; //設置是否隨機重命名文件, false不隨機

private $originName; //源文件名
private $tmpFileName; //臨時文件名
private $fileType; //文件類型(文件後綴)
private $fileSize; //文件大小
private $newFileName; //新文件名
private $errorNum = 0; //錯誤號
private $errorMess=""; //錯誤報告消息

/**
* 用於設置成員屬性($path, $allowtype,$maxsize, $israndname)
* 可以通過連貫操作一次設置多個屬性值
*@param string $key 成員屬性名(不區分大小寫)
*@param mixed $val 為成員屬性設置的值
*@return object 返回自己對象$this,可以用於連貫操作
*/
function set($key, $val){
$key = strtolower($key);
if( array_key_exists( $key, get_class_vars(get_class($this) ) ) ){
$this->setOption($key, $val);
}
return $this;
}

/**
* 調用該方法上傳文件
* @param string $fileFile 上傳文件的表單名稱
* @return bool 如果上傳成功返回數true
*/

function upload($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'];

/* 如果是多個文件上傳則$file["name"]會是一個數組 */
if(is_Array($name)){
$errors=array();
/*多個文件上傳則循環處理 , 這個循環只有檢查上傳文件的作用,並沒有真正上傳 */
for($i = 0; $i < count($name); $i++){
/*設置文件信息 */
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) {
if(!$this->checkFileSize() || !$this->checkFileType()){
$errors[] = $this->getError();
$return=false;
}
}else{
$errors[] = $this->getError();
$return=false;
}
/* 如果有問題,則重新初使化屬性 */
if(!$return)
$this->setFiles();
}

if($return){
/* 存放所有上傳後文件名的變數數組 */
$fileNames = array();
/* 如果上傳的多個文件都是合法的,則通過銷魂循環向伺服器上傳文件 */
for($i = 0; $i < count($name); $i++){
if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i] )) {
$this->setNewFileName();
if(!$this->File()){
$errors[] = $this->getError();
$return = false;
}
$fileNames[] = $this->newFileName;
}
}
$this->newFileName = $fileNames;
}
$this->errorMess = $errors;
return $return;
/*上傳單個文件處理方法*/
} else {
/* 設置文件信息 */
if($this->setFiles($name,$tmp_name,$size,$error)) {
/* 上傳之前先檢查一下大小和類型 */
if($this->checkFileSize() && $this->checkFileType()){
/* 為上傳文件設置新文件名 */
$this->setNewFileName();
/* 上傳文件 返回0為成功, 小於0都為錯誤 */
if($this->File()){
return true;
}else{
$return=false;
}
}else{
$return=false;
}
} else {
$return=false;
}
//如果$return為false, 則出錯,將錯誤信息保存在屬性errorMess中
if(!$return)
$this->errorMess=$this->getError();

return $return;
}
}

/**
* 獲取上傳後的文件名稱
* @param void 沒有參數
* @return string 上傳後,新文件的名稱, 如果是多文件上傳返回數組
*/
public function getFileName(){
return $this->newFileName;
}

/**
* 上傳失敗後,調用該方法則返回,上傳出錯信息
* @param void 沒有參數
* @return string 返回上傳文件出錯的信息報告,如果是多文件上傳返回數組
*/
public function getErrorMsg(){
return $this->errorMess;
}

/* 設置上傳出錯信息 */
private function getError() {
$str = "上傳文件<font color='red'>{$this->originName}</font>時出錯 : ";
switch ($this->errorNum) {
case 4: $str .= "沒有文件被上傳"; break;
case 3: $str .= "文件只有部分被上傳"; break;
case 2: $str .= "上傳文件的大小超過了HTML表單中MAX_FILE_SIZE選項指定的值"; break;
case 1: $str .= "上傳的文件超過了php.ini中upload_max_filesize選項限制的值"; 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>';
}

/* 設置和$_FILES有關的內容 */
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);
$aryStr = explode(".", $name);
$this->setOption('fileType', strtolower($aryStr[count($aryStr)-1]));
$this->setOption('fileSize', $size);
return true;
}

/* 為單個成員屬性設置值 */
private function setOption($key, $val) {
$this->$key = $val;
}

/* 設置上傳後的文件名稱 */
private function setNewFileName() {
if ($this->israndname) {
$this->setOption('newFileName', $this->proRandName());
} else{
$this->setOption('newFileName', $this->originName);
}
}

/* 檢查上傳的文件是否是合法的類型 */
private function checkFileType() {
if (in_array(strtolower($this->fileType), $this->allowtype)) {
return true;
}else {
$this->setOption('errorNum', -1);
return false;
}
}

/* 檢查上傳的文件是否是允許的大小 */
private function checkFileSize() {
if ($this->fileSize > $this->maxsize) {
$this->setOption('errorNum', -2);
return false;
}else{
return true;
}
}

/* 檢查是否有存放上傳文件的目錄 */
private function checkFilePath() {
if(empty($this->path)){
$this->setOption('errorNum', -5);
return false;
}
if (!file_exists($this->path) || !is_writable($this->path)) {
if (!@mkdir($this->path, 0755)) {
$this->setOption('errorNum', -4);
return false;
}
}
return true;
}

/* 設置隨機文件名 */
private function proRandName() {
$fileName = date('YmdHis')."_".rand(100,999);
return $fileName.'.'.$this->fileType;
}

/* 復制上傳文件到指定的位置 */
private function File() {
if(!$this->errorNum) {
$path = rtrim($this->path, '/').'/';
$path .= $this->newFileName;
if (@move_uploaded_file($this->tmpFileName, $path)) {
return true;
}else{
$this->setOption('errorNum', -3);
return false;
}
} else {
return false;
}
}
}

四、文件上傳類的應用過程

本例的文件上傳類FileUpload,即支持單文件上傳,也支持多個文件一起向伺服器上傳,在處理方式上沒有區別的,只不過在編寫上傳標單時,多個文件上傳一定要以數組方式傳遞給伺服器。單個文件上傳表單如下所示:
<form action="upload.php" method="post" enctype="multipart/form-data" >
name: <input type="text" name="username" value="" /><br>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
up pic: <input type="file" name="pic[]" value=""><br>
up pic: <input type="file" name="pic[]" value=""><br>
up pic: <input type="file" name="pic[]" value=""><br>
up pic: <input type="file" name="pic[]" value=""><br>

<input type="submit" value="upload" /><br>

</form>

上面表單,都將提交的位置指向了同一個文件upload.php,所以不難看出單個和多個文件上傳是一樣的處理方式,upload.php代碼如下所示:

<?php
//包含一個文件上傳類中的上傳類
include "fileupload.class.php";

$up = new fileupload;
//設置屬性(上傳的位置, 大小, 類型, 名是是否要隨機生成)
$up -> set("path", "./images/");
$up -> set("maxsize", 2000000);
$up -> set("allowtype", array("gif", "png", "jpg","jpeg"));
$up -> set("israndname", false);

//使用對象中的upload方法, 就可以上傳文件, 方法需要傳一個上傳表單的名子 pic, 如果成功返回true, 失敗返回false
if($up -> upload("pic")) {
echo '<pre>';
//獲取上傳後文件名子
var_mp($up->getFileName());
echo '</pre>';

} else {
echo '<pre>';
//獲取上傳失敗以後的錯誤提示
var_mp($up->getErrorMsg());
echo '</pre>';
}
?>

❾ php上傳如何替換原圖片文件

string filename = FileUpload1.FileName; //獲取上傳的文件名
string fileup = Server.MapPath("\\Web\\images\\" + filename); //獲取伺服器保存文件的路徑
string filetype = FileUpload1.PostedFile.ContentType; //獲取文件類型,做判斷用
string fileclass= filename.Substring(filename.LastIndexOf(".")+1); //獲取文件擴展名,做判斷用
if (fileclass == "gif") //判斷擴展名
{
if (filetype == "image/gif") //判斷類型
{
FileUpload1.SaveAs(fileup); //上傳到伺服器中
if (File.Exists("\\Web\\images\\logo.gif")) //判斷如果伺服器中這個路徑下存在logo.gif文件
{
File.Delete("\\Web\\images\\logo.gif"); //那麼就將它刪除
}
File.Move("\\Web\\images\\" + filename, "\\Web\\images\\logo.gif"); //把上傳上來的文件重命名為logo.gif
Image1.ImageUrl = "/Web/images/logo.gif"; //讓image1控制項顯示上傳上來的文件
}
else
{
Response.Write("<script>alert('系統檢測到上傳的文件非法格式!');</script>"); //如果類型不正確提示
}
}
else
{
Response.Write("<script>alert('上傳的文件格式不正確!');</script>"); //如果擴展名不正確提示
}

❿ 怎麼用php做一個文件上傳代碼實例

<?php
/**
file:fileupload.class.php文件上傳類FileUpload
本類的實例對象用於處理上傳文件,可以上傳一個文件,也可同時處理多個文件上傳
*/
classFileUpload{
private$path="./uploads";//上傳文件保存的路徑
private$allowtype=array('jpg','gif','png');//設置限制上傳文件的類型
private$maxsize=1000000;//限制文件上傳大小(位元組)
private$israndname=true;//設置是否隨機重命名文件,false不隨機

private$originName;//源文件名
private$tmpFileName;//臨時文件名
private$fileType;//文件類型(文件後綴)
private$fileSize;//文件大小
private$newFileName;//新文件名
private$errorNum=0;//錯誤號
private$errorMess="";//錯誤報告消息

/**
*用於設置成員屬性($path,$allowtype,$maxsize,$israndname)
*可以通過連貫操作一次設置多個屬性值
*@paramstring$key成員屬性名(不區分大小寫)
*@parammixed$val為成員屬性設置的值
*@returnobject返回自己對象$this,可以用於連貫操作
*/
functionset($key,$val){
$key=strtolower($key);
if(array_key_exists($key,get_class_vars(get_class($this)))){
$this->setOption($key,$val);
}
return$this;
}

/**
*調用該方法上傳文件
*@paramstring$fileFile上傳文件的表單名稱
*@returnbool如果上傳成功返回數true
*/

functionupload($fileField){
$return=true;
/*檢查文件路徑是滯合法*/
if(!$this->checkFilePath()){
$this->errorMess=$this->getError();
returnfalse;
}
/*將文件上傳的信息取出賦給變數*/
$name=$_FILES[$fileField]['name'];
$tmp_name=$_FILES[$fileField]['tmp_name'];
$size=$_FILES[$fileField]['size'];
$error=$_FILES[$fileField]['error'];

/*如果是多個文件上傳則$file["name"]會是一個數組*/
if(is_Array($name)){
$errors=array();
/*多個文件上傳則循環處理,這個循環只有檢查上傳文件的作用,並沒有真正上傳*/
for($i=0;$i<count($name);$i++){
/*設置文件信息*/
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
if(!$this->checkFileSize()||!$this->checkFileType()){
$errors[]=$this->getError();
$return=false;
}
}else{
$errors[]=$this->getError();
$return=false;
}
/*如果有問題,則重新初使化屬性*/
if(!$return)
$this->setFiles();
}

if($return){
/*存放所有上傳後文件名的變數數組*/
$fileNames=array();
/*如果上傳的多個文件都是合法的,則通過銷魂循環向伺服器上傳文件*/
for($i=0;$i<count($name);$i++){
if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i])){
$this->setNewFileName();
if(!$this->File()){
$errors[]=$this->getError();
$return=false;
}
$fileNames[]=$this->newFileName;
}
}
$this->newFileName=$fileNames;
}
$this->errorMess=$errors;
return$return;
/*上傳單個文件處理方法*/
}else{
/*設置文件信息*/
if($this->setFiles($name,$tmp_name,$size,$error)){
/*上傳之前先檢查一下大小和類型*/
if($this->checkFileSize()&&$this->checkFileType()){
/*為上傳文件設置新文件名*/
$this->setNewFileName();
/*上傳文件返回0為成功,小於0都為錯誤*/
if($this->File()){
returntrue;
}else{
$return=false;
}
}else{
$return=false;
}
}else{
$return=false;
}
//如果$return為false,則出錯,將錯誤信息保存在屬性errorMess中
if(!$return)
$this->errorMess=$this->getError();

return$return;
}
}

/**
*獲取上傳後的文件名稱
*@paramvoid沒有參數
*@returnstring上傳後,新文件的名稱,如果是多文件上傳返回數組
*/
publicfunctiongetFileName(){
return$this->newFileName;
}

/**
*上傳失敗後,調用該方法則返回,上傳出錯信息
*@paramvoid沒有參數
*@returnstring返回上傳文件出錯的信息報告,如果是多文件上傳返回數組
*/
publicfunctiongetErrorMsg(){
return$this->errorMess;
}

/*設置上傳出錯信息*/
privatefunctiongetError(){
$str="上傳文件<fontcolor='red'>{$this->originName}</font>時出錯:";
switch($this->errorNum){
case4:$str.="沒有文件被上傳";break;
case3:$str.="文件只有部分被上傳";break;
case2:$str.="上傳文件的大小超過了HTML表單中MAX_FILE_SIZE選項指定的值";break;
case1:$str.="上傳的文件超過了php.ini中upload_max_filesize選項限制的值";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>';
}

/*設置和$_FILES有關的內容*/
privatefunctionsetFiles($name="",$tmp_name="",$size=0,$error=0){
$this->setOption('errorNum',$error);
if($error)
returnfalse;
$this->setOption('originName',$name);
$this->setOption('tmpFileName',$tmp_name);
$aryStr=explode(".",$name);
$this->setOption('fileType',strtolower($aryStr[count($aryStr)-1]));
$this->setOption('fileSize',$size);
returntrue;
}

/*為單個成員屬性設置值*/
privatefunctionsetOption($key,$val){
$this->$key=$val;
}

/*設置上傳後的文件名稱*/
privatefunctionsetNewFileName(){
if($this->israndname){
$this->setOption('newFileName',$this->proRandName());
}else{
$this->setOption('newFileName',$this->originName);
}
}

/*檢查上傳的文件是否是合法的類型*/
privatefunctioncheckFileType(){
if(in_array(strtolower($this->fileType),$this->allowtype)){
returntrue;
}else{
$this->setOption('errorNum',-1);
returnfalse;
}
}

/*檢查上傳的文件是否是允許的大小*/
privatefunctioncheckFileSize(){
if($this->fileSize>$this->maxsize){
$this->setOption('errorNum',-2);
returnfalse;
}else{
returntrue;
}
}

/*檢查是否有存放上傳文件的目錄*/
privatefunctioncheckFilePath(){
if(empty($this->path)){
$this->setOption('errorNum',-5);
returnfalse;
}
if(!file_exists($this->path)||!is_writable($this->path)){
if(!@mkdir($this->path,0755)){
$this->setOption('errorNum',-4);
returnfalse;
}
}
returntrue;
}

/*設置隨機文件名*/
privatefunctionproRandName(){
$fileName=date('YmdHis')."_".rand(100,999);
return$fileName.'.'.$this->fileType;
}

/*復制上傳文件到指定的位置*/
privatefunctionFile(){
if(!$this->errorNum){
$path=rtrim($this->path,'/').'/';
$path.=$this->newFileName;
if(@move_uploaded_file($this->tmpFileName,$path)){
returntrue;
}else{
$this->setOption('errorNum',-3);
returnfalse;
}
}else{
returnfalse;
}
}
}

閱讀全文

與phpfileuploads相關的資料

熱點內容
愛上北斗星男友在哪個app上看 瀏覽:413
主力散戶派發源碼 瀏覽:663
linux如何修復伺服器時間 瀏覽:55
榮縣優途網約車app叫什麼 瀏覽:472
百姓網app截圖是什麼意思 瀏覽:222
php如何嵌入html 瀏覽:809
解壓專家怎麼傳輸 瀏覽:743
如何共享伺服器的網路連接 瀏覽:132
程序員簡易表白代碼 瀏覽:166
什麼是無線加密狗 瀏覽:62
國家反詐中心app為什麼會彈出 瀏覽:67
cad壓縮圖列印 瀏覽:102
網頁打開速度與伺服器有什麼關系 瀏覽:863
android開發技術文檔 瀏覽:65
32單片機寫程序 瀏覽:51
三星雙清無命令 瀏覽:839
漢壽小程序源碼 瀏覽:344
易助erp雲伺服器 瀏覽:533
修改本地賬戶管理員文件夾 瀏覽:420
python爬蟲工程師招聘 瀏覽:287