导航:首页 > 编程语言 > 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相关的资料

热点内容
汉寿小程序源码 浏览:340
易助erp云服务器 浏览:530
修改本地账户管理员文件夹 浏览:416
python爬虫工程师招聘 浏览:283
小鹏p7听音乐哪个app好 浏览:354
linux下的防火墙 浏览:954
凌达压缩机美芝压缩机 浏览:350
php后面代码不执行 浏览:236
微我手机怎样设置应用加密 浏览:202
条件加密 浏览:628
androidstudio设置中文 浏览:641
汽车换压缩机能提升制冷 浏览:628
安卓开发配什么电脑 浏览:607
linux下php模块 浏览:78
阿里云服务器终端在哪里 浏览:148
app纸有什么用 浏览:224
cuteftp命令 浏览:507
最开始的编程语言是什么 浏览:760
at远程命令 浏览:493
云服务器哪家好点 浏览:215