php文件上传代码|求PHP网页上传文件代码

php文件上传代码|求PHP网页上传文件代码的第1张示图

❶ 求一段php上传任意文件的代码

PHP 文件上传 是通过 PHP,可以把文件上传到服务器。1首先创建一个文件上传表单允许用户从表单上传文件是非常有用的。请看下面这个供上传文件的 HTML 表单:<html><body><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></body></html>请留意如下有关此表单的信息:<form> 标签的 enctype 属性规定了在提交表单时要使用哪种内容类型。在表单需要二进制数据时,比如文件内容,请使用 "multipart/form-data"。<input> 标签的 type="file" 属性规定了应该把输入作为文件来处理。举例来说,当在浏览器中预览时,会看到输入框旁边有一个浏览按钮。注释:允许用户上传文件是一个巨大的安全风险。请仅仅允许可信的用户执行文件上传操作。2:创建上传脚本"upload_file.php" 文件含有供上传文件的代码:<?phpif ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; }else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; }?>通过使用 PHP 的全局数组 $_FILES,你可以从客户计算机向远程服务器上传文件。第一个参数是表单的 input name,第二个下标可以是 "name", "type", "size", "tmp_name" 或 "error"。就像这样:$_FILES["file"]["name"] – 被上传文件的名称$_FILES["file"]["type"] – 被上传文件的类型$_FILES["file"]["size"] – 被上传文件的大小,以字节计$_FILES["file"]["tmp_name"] – 存储在服务器的文件的临时副本的名称$_FILES["file"]["error"] – 由文件上传导致的错误代码这是一种非常简单文件上传方式。不需要考虑文件的上传格式。

❷ php多文件上传实现代码

这篇文章主要介绍了php多文件上传实现代码,需要的朋友可以参考下index_uploads.php 代码如下:<html><head><metacharset="utf-8"><title>index_uploads</title></head><body><formaction="uploads.php"method="post"enctype="multipart/form-data"><inputtype="file"name="file[]"><br><inputtype="file"name="file[]"><br><inputtype="submit"value="uploads"></form></body></html>uploads.php 代码如下:<?phpheader("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"<script>alert('succeed!');window.location.href='index_uploads.php';</script>";}else{echo"<script>alert('fail!');window.location.href='index_uploads.php';</script>";}}核心:<1>上传首页中input的name属性是这么设置的。 <2>用while循环上传多文件。

❸ 求php上传文件代码

求php上传文件代码疗献砌挠奇胀垦可压含双李聋修烧新婚总少不了祝福,我们对新人的祝福饱含了对他们将来的祝愿和期望,也希望我们的祝福能给新人带来幸福,来看看我们准备的祝福语吧…

❹ php上传文件代码,怎么写

2楼和3楼给你的回答就跟SB一样,自己比你还菜,就瞎扯。我问问题的时候就是被这些SB不停骚扰导致最后无人回答了。 上传代码看这里(带重命名的):<form action="" method="post" enctype="multipart/form-data">上传文件:<input type="file" name="upLoad"><br>重命名为:<input type="text" name="newName"><br><input type="submit" name="submit"><input type="reset" name="reset"></form><br><i><small>文件上传到服务器需要一些时间</small></i><br><?php$upLoadDir="/"; //为目录变量指定目录位置$upLoadError=$_FILES['upLoad']['error'];$fileName=$_FILES['upLoad']['name'];$fileTemName=$_FILES['upLoad']['tmp_name'];$fileSize=$_FILES['upLoad']['size'];$newName=$_POST['newName'];function upLoad(){ global $upLoadDir,$upLoadError,$fileName,$fileTemName,$fileSize,$fileSuffix,$newName; if($newName){ //如果需要被更新文件名 $fileReName=$newName.".".pathinfo($fileName,PATHINFO_EXTENSION); //采用新文件名+获取文件名后缀 }else{ //如果不需要更新文件名 $fileReName=$fileName; //定义文件存储位置,并在文件名前加一组随机数字 } if($upLoadError>0){ //0表示没有错误发生,文件上传成功 echo"错误:"; switch($upLoadError){ case 1:echo"上传文件超过配置文件规定值。";break; //1表示上传的文件超过了php.ini中upload_max_filesize选项限制的值 case 2:echo"上传文件超过表单约定值。";break; //2表示上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 case 3:echo"上传文件不完全。";break; //3表示文件只有部分被上传。 case 4:echo"没有上传文件。";break; //4表示没有文件被上传。 } }else{ if(is_uploaded_file($fileTemName)){ //确认文件通过HTTP POST上传 if(!move_uploaded_file($fileTemName,($upLoadDir.$fileReName))){ //如果无法将上传的文件移动到新位置 echo"文件上传失败,请重新上传。"; }else{ //否则返回成功信息 echo"文件上传成功!<br>".date("Y-m-d H:i:s")."<br>上传文件:".$fileName."<br>文件大小:".number_format(($fileSize/1024/1024),2)."Mb"."<br>重命名为:".$fileReName; } }else{ //如果不是通过HTTP POST方式上传,则提示非法信息 echo"文件".$fileName."不合法!"; } }}if(!empty($fileName)){ if(is_dir($upLoadDir)){ //如果目录存在 upLoad(); //则执行上传流程 }else{ //如果目录不存在 mkdir($upLoadDir); //则创建目录 upLoad(); //再执行上传流程 }}else{ echo"请选择需要上传的文件。";}?>

❺ 求PHP网页上传文件代码.

自定义文件上传函数:<?php/** * 文件上传 *//** * 文件上传函数 * @param $name string 表单上传框的name值 * @param $file_arr array 上传文件的类型限制 *@param $filesize int 上传文件限制其大小 * @param $path string 文件的保存路径 * @return string 成功返回 "上传成功及文件名" */ function uploads($name,$file_arr=array('jpg','png','gif','JPG','GIF','PNG'),$filesize=3145728,$path='uploads'){//1、检测文件的错误信息,如果是0 就允许上传(保存)$err=$_FILES[$name]['error'];if($err>0){if($err==1){return '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。';}elseif($err==2){return '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 ';}elseif($err==3){return '文件只有部分被上传。';}elseif($err==4){return'没有文件上传';}elseif($err>=5){return '其他错误';}}//2、检测文件的类型,是否是我们需要的(png、gif、jpg)$pre=pathinfo($_FILES[$name]['name'],PATHINFO_EXTENSION);if(!in_array($pre,$file_arr)){return '上传的文件类型不符';}//3、检测文件大小if($_FILES[$name]['size']>$filesize){return '上传的文件太大';}//4、保存文件$file_name=date('YmdHis',time()).mt_rand(1000,9999).'.'.$pre;if(is_uploaded_file($_FILES[$name]['tmp_name'])){move_uploaded_file($_FILES[$name]['tmp_name'], $path."/".$file_name);return '上传成功|'.$file_name;}else{return "文件上传失败!";} }?>

❻ php上传文件代码,能用的代码

<?php$uptypes=array('image/jpg', //上传文件类型列表'image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'); $max_file_size=5000000; //上传文件大小限制, 单位BYTE$destination_folder="upload/"; //上传文件路径$watermark=1; //是否附加水印(1为加水印,其他为不加水印);$watertype=1; //水印类型(1为文字,2为图片)$waterposition=1; //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);$waterstring="newphp.site.cz"; //水印字符串$waterimg="xplore.gif"; //水印图片$imgpreview=1; //是否生成预览图(1为生成,其他为不生成);$imgpreviewsize=1/2; //缩略图比例?><html><head><title>M4U BLOG – fywyj.cn</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><style type="text/css">body,td{font-family:tahoma,verdana,arial;font-size:11px;line-height:15px;background-color:white;color:#666666;margin-left:20px;}strong{font-size:12px;}aink{color:#0066CC;}a:hover{color:#FF6600;}aisited{color:#003366;}a:active{color:#9DCC00;}table.itable{}td.irows{height:20px;background:url("index.php?i=dots" repeat-x bottom}</style></head><body><center><form enctype="multipart/form-data" method="post" name="upform">上传文件: <br><br><br><input name="upfile" type="file" style="width:200;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17"><input type="submit" value="上传" style="width:30;border:1 solid #9a9999; font-size:9pt; background-color:#ffffff" size="17"><br><br><br>允许上传的文件类型为:jpg|jpeg|png|pjpeg|gif|bmp|x-png|swf <br><br><a href="index.php">返回</a></form><?phpif ($_SERVER['REQUEST_METHOD'] == 'POST'){if (!is_uploaded_file($_FILES["upfile"][tmp_name]))//是否存在文件{ echo "<font color='red'>文件不存在!</font>";exit;}$file = $_FILES["upfile"];if($max_file_size < $file["size"])//检查文件大小{echo "<font color='red'>文件太大!</font>";exit; }if(!in_array($file["type"], $uptypes))//检查文件类型{echo "<font color='red'>只能上传图像文件或Flash!</font>";exit; }if(!file_exists($destination_folder))mkdir($destination_folder);$filename=$file["tmp_name"];$image_size = getimagesize($filename); $pinfo=pathinfo($file["name"]);$ftype=$pinfo[extension];$destination = $destination_folder.time().".".$ftype;if (file_exists($destination) && $overwrite != true){ echo "<font color='red'>同名文件已经存在了!</a>"; exit; }if(!move_uploaded_file ($filename, $destination)){ echo "<font color='red'>移动文件出错!</a>"; exit; }$pinfo=pathinfo($destination);$fname=$pinfo[basename];echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";echo " 宽度:".$image_size[0];echo " 长度:".$image_size[1];if($watermark==1){$iinfo=getimagesize($destination,$iinfo);$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);$white=imagecolorallocate($nimage,255,255,255);$black=imagecolorallocate($nimage,0,0,0);$red=imagecolorallocate($nimage,255,0,0);imagefill($nimage,0,0,$white);switch ($iinfo[2]){case 1:$simage =imagecreatefromgif($destination);break;case 2:$simage =imagecreatefromjpeg($destination);break;case 3:$simage =imagecreatefrompng($destination);break;case 6:$simage =imagecreatefromwbmp($destination);break;default:die("<font color='red'>不能上传此类型文件!</a>");exit;}image($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);switch($watertype){case 1: //加水印字符串imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);break;case 2: //加水印图片$simage1 =imagecreatefromgif("xplore.gif");image($nimage,$simage1,0,0,0,0,85,15);imagedestroy($simage1);break;} switch ($iinfo[2]){case 1://imagegif($nimage, $destination);imagejpeg($nimage, $destination);break;case 2:imagejpeg($nimage, $destination);break;case 3:imagepng($nimage, $destination);break;case 6:imagewbmp($nimage, $destination);//imagejpeg($nimage, $destination);break;}//覆盖原上传文件imagedestroy($nimage);imagedestroy($simage);}if($imgpreview==1){echo "<br>图片预览:<br>";echo "<a href=\"".$destination."\" target='_blank'><img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\" border='0'></a>";}}?></center></body></html>绝对好用!!!!给分吧

❼ 求一php文件上传代码,需要获取到文件的路径。

第二个这样写<?php$action = $_GET["action"];if($action == "insertRoom"){$rType = $_POST["rType"];$rPrice = $_POST["rPrice"];$rPicurl = $_POST["rPicurl"]; echo $rpicurl; echo "<br>"; echo $rType;$pic = $_FILES["rPicurl"]; //接收文件上传foreach($pic as $key=>$val){echo "{$key}=>";print_r($pic[$key]); //打印数组echo "<br>";}echo "**********<br>";foreach($pic["name"] as $key=>$val){echo $key."<br>"; //如果库key==0,则$pic[""][$key] 都是指向第一图片的if($pic["error"][$key]==0){ //此时指向第一个文件$newName = "upload/".getNewName().".".getExtName($pic["name"][$key]);move_uploaded_file($pic["tmp_name"][$key],$newName); //把上传来的图片从临时位置移动到指定位置。}function getExtName($file){ //获得文件括展名$ext = pathinfo($file);print_r($ext);return $ext["extension"];}function getNewName(){$newName = date("YmdHis").md5(mt_rand(10000,45333));return $newName;}第三个不要了,合并了,可能变量名称会有错,自己调试下

❽ 怎么用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([email protected]($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;}}}

❾ 求php文件上传代码

上传的同时 是不可能被确认的 你只有先把文件放到一个目录中 管理员通过以后再移动到另外回的目录里去 或者文件显答示给用户的时候 是通过数据库的存储信息来进行确认的 也就是说 数据库中标识的没有通过的文件 则不可以被显示给普通用户。至于文件的名称 文件的大小 和扩展什么的 都是可以读取 并保存起来的

❿ 谁有文件上传的PHP后台代码

<?php$up=$_FILES["files"];if($up["error"]>0){//获取错误信息switch($up['error']){case 1:$info="<h2>上传的文件超过了php.ini中upload_max_filesize选项限制的值</h2>";break;case 2:$info="上传的文件超过了html中MAX_FILE_SIZE选项指定的值";你如果还有不明白的,可以去后盾人看看相关视频教材,多看几遍不就会了,我就是这样的。

未经允许不得转载:山九号 » php文件上传代码|求PHP网页上传文件代码

赞 (0)