代码如下 | 复制代码 |
<?php |
使用方法
代码如下 | 复制代码 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>上传文件</title></head><body leftmargin="0" topmargin="0"><table cellpadding="2" cellspacing="1" border="0" height="100%" align="left"><form action='wend.php?action=upload' method='post' enctype='multipart/form-data'><tr ><td valign='middle'><input type='file' name='uploadfile'><input name='submit' type='submit' value='上传'></td></tr></form></table</body></html> |
php处理文件
调用上面的类文件,然后再如下操作
代码如下 | 复制代码 |
$action = addslashes($_GET['action']);
|
最简单的实例如下
代码如下 | 复制代码 |
<form action="" method="post" enctype="multipart/form-data"> <?php
|
下面分享其它朋友的例子
例1
代码如下 | 复制代码 |
<? |
如果我们要动态不确定性的多文件上传怎么实现下面也有实例
文件上传代码
代码如下 | 复制代码 |
view plaincopy to clipboardprint? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>文档上传</title> </head> <body> <script language="javascript"><!-- 动态添加文件选择控件--> function AddRow() { var eNewRow = tblData.insertRow(); for (var i=0;i<1;i++) { var eNewCell = eNewRow.insertCell(); eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>"; } } // --></script> <form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" > <table id="tblData" width="400" border="0"> <!-- 将上传文件必须用post的方法和enctype="multipart/form-data" --> <!-- 将本页的网址传给uploadfile.php--> <input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" /> <tr><td>文件上传列表 <input type="button" name="addfile" onclick="AddRow()" value="添加列表" /></td></tr> <!-- filelist[]必须是一个数组--> <tr><td><input type="file" name="filelist[]" size="50" /></td></tr> </table> <input type="submit" name="submitfile" value="提交文件" /> </form> </body> </html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>文档上传</title> </head> <body> <script language="javascript"><!-- 动态添加文件选择控件--> function AddRow() { var eNewRow = tblData.insertRow(); for (var i=0;i<1;i++) { var eNewCell = eNewRow.insertCell(); eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>"; } } // --></script> <form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" > <table id="tblData" width="400" border="0"> <!-- 将上传文件必须用post的方法和enctype="multipart/form-data" --> <!-- 将本页的网址传给uploadfile.php--> <input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" /> <tr><td>文件上传列表 <input type="button" name="addfile" onclick="AddRow()" value="添加列表" /></td></tr> <!-- filelist[]必须是一个数组--> <tr><td><input type="file" name="filelist[]" size="50" /></td></tr> </table> <input type="submit" name="submitfile" value="提交文件" /> </form> </body> </html> 提交文件代码 view plaincopy to clipboardprint? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>文件上传结果</title> </head> <body> <?php if ($_POST["submitfile"]!="") { $Path="./".date('Ym')."/"; if (!is_dir($Path))//创建路径 { mkdir($Path); } echo "<div>"; for ($i=0;$i<count($filelist);$i++) { //$_FILES["filelist"]["size"][$i]的排列顺序不可以变,因为fileist是一个二维数组 if ($_FILES["filelist"]["size"][$i]!=0) { $File=$Path.date('Ymdhm')."_".$_FILES["filelist"]["name"][$i]; if (move_uploaded_file($_FILES["filelist"]["tmp_name"][$i],$File)) { echo "文件上传成功 文件类型:".$_FILES["filelist"]["type"][$i]." "."文件名:" .$_FILES["filelist"]["name"][$i]."<br>"; } else { echo "文件名:".$_FILES["filelist"]["name"][$i]."上传失败</br>"; } } } echo "</div><br><a href="$postadd" href="$postadd">返回</a></div>"; } ?> </body> </html> |
另:错误信息说明
从 PHP 4.2.0 开始,PHP 将随文件信息数组一起返回一个对应的错误代码。该代码可以在文件上传时生成的文件数组中的 error 字段中被找到,也就是 $_FILES['userfile']['error']。
UPLOAD_ERR_OK
其值为 0,没有错误发生,文件上传成功。
UPLOAD_ERR_INI_SIZE
其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
UPLOAD_ERR_FORM_SIZE
其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 UPLOAD_ERR_PARTIAL
其值为 3,文件只有部分被上传。
UPLOAD_ERR_NO_FILE
其值为 4,没有文件被上传。
UPLOAD_ERR_NO_TMP_DIR
其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。
UPLOAD_ERR_CANT_WRITE
其值为 7,文件写入失败。PHP 5.1.0 引进。
注意: 以上值在 PHP 4.3.0 之后变成了 PHP 常量。
代码如下 | 复制代码 |
<?php // 1. 定义各个成员 有宽、高、画布、字数、类型、画类型 private $width; //宽度 public function __construct($height=20,$num=4,$imgType="jpeg",$Type=1){ // 2. 定义随机获取字符串函数 case 1: // 类型为1 获取1-9随机数
// 4. 生成背景颜色 // 5. 生成字体颜色 // 6. 填充背景颜色 // 7. 干扰点 // 8. 随机直线 弧线 // 9. 写字 // 10. 输出 // 11. 拼装 public function getCodeStr(){ |
例1
代码如下 | 复制代码 |
/* |
例2
代码如下 | 复制代码 |
<?php // function GrabImage($url,$filename="") { if($filename=="") { ob_start(); $fp2=@fopen($filename, "a"); return $filename;
?> |
dedecms中的:
代码如下 | 复制代码 |
if(!empty($saveremoteimg)) { $body = stripslashes($body); $img_array = array(); preg_match_all("/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array); $img_array = array_unique($img_array[2]); set_time_limit(0); $imgUrl = $img_dir."/".strftime("%Y%m%d",time()); $imgPath = $base_dir.$imgUrl; $milliSecond = strftime("%H%M%S",time()); if(!is_dir($imgPath)) @mkdir($imgPath,0777); foreach($img_array as $key =>$value) { $value = trim($value); $get_file = @file_get_contents($value); $rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3); $fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3); if($get_file) { $fp = @fopen($rndFileName,"w"); @fwrite($fp,$get_file); @fclose($fp); } $body = ereg_replace($value,$fileurl,$body); } $body = addslashes($body); } ?> |
例4
代码如下 | 复制代码 |
<?php |
最简单的js实现方法
代码如下 | 复制代码 |
<script language="JavaScript"> function toDesktop(sUrl,sName){ try { var WshShell = new ActiveXObject("WScript.Shell"); var oUrlLink = WshShell.CreateShortcut(WshShell.SpecialFolders("Desktop") + "\" + sName + ".url"); oUrlLink.TargetPath = sUrl; oUrlLink.Save(); } catch(e) { alert("请点击弹出对话框的:是 "); } } </script> <input name="btn" type="button" id="btn" value="把百度创建快捷方式到桌面" onClick="toDesktop('http://www.111cn.net/','百度一下,你就知道!')"> <input name="btn" type="button" id="btn" value="C盘" onClick="toDesktop('file://C:','C盘')"> |
不足:这样做如果浏览器做了安全设置我们是不能使用上面的方法的。
写php程序的朋友可能也知道一种办法,代码如下
代码如下 | 复制代码 |
<?php $Shortcut = "[InternetShortcut] URL=http://www.111cn.net IconFile=http://www.111cn.net/favicon.ico IconIndex=0 HotKey=1613 IDList= [{000214A0-0000-0000-C000-000000000046}] Prop3=19,2"; header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=蜕变无忧.url"); echo $Shortcut; ?>
|
asp.net程序员可能也知道如下代码
代码如下 | 复制代码 |
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class CreateShortcut : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } /// <summary> /// 创建快捷方式 /// </summary> /// <param name="Title">标题</param> /// <param name="URL">URL地址</param> private void CreateShortcut(string Title, string URL) { string strFavoriteFolder; // “收藏夹”中 创建 IE 快捷方式 strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites); CreateShortcutFile(Title, URL, strFavoriteFolder); // “ 桌面 ”中 创建 IE 快捷方式 strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop); CreateShortcutFile(Title, URL, strFavoriteFolder); // “ 链接 ”中 创建 IE 快捷方式 strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\链接"; CreateShortcutFile(Title, URL, strFavoriteFolder); //「开始」菜单中 创建 IE 快捷方式 strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu); CreateShortcutFile(Title, URL, strFavoriteFolder); } /// <summary> /// 创建快捷方式 /// </summary> /// <param name="Title">标题</param> /// <param name="URL">URL地址</param> /// <param name="SpecialFolder">特殊文件夹</param> private void CreateShortcutFile(string Title, string URL, string SpecialFolder) { // Create shortcut file, based on Title System.IO.StreamWriter objWriter = System.IO.File.CreateText(SpecialFolder + "\" + Title + ".url"); // Write URL to file objWriter.WriteLine("[InternetShortcut]"); objWriter.WriteLine("URL=" + URL); // Close file objWriter.Close(); } private void btnShortcut_Click(object sender, System.EventArgs e) { CreateShortcut("脚本之家", http://www.111cn.net); } } |