<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>Javascript</title><link>http://blog.blueshop.com.tw/guancheng713/category/2402.aspx</link><description>Javascript</description><managingEditor>Aery</managingEditor><dc:language>zh-TW</dc:language><generator>.Text Version 0.95.2004.101</generator><item><dc:creator>Aery</dc:creator><title>用php下載指定檔案並重新命名(另存新檔)</title><link>http://blog.blueshop.com.tw/guancheng713/archive/2007/12/24/53816.aspx</link><pubDate>Mon, 24 Dec 2007 18:16:00 GMT</pubDate><guid>http://blog.blueshop.com.tw/guancheng713/archive/2007/12/24/53816.aspx</guid><wfw:comment>http://blog.blueshop.com.tw/guancheng713/comments/53816.aspx</wfw:comment><comments>http://blog.blueshop.com.tw/guancheng713/archive/2007/12/24/53816.aspx#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://blog.blueshop.com.tw/guancheng713/comments/commentRss/53816.aspx</wfw:commentRss><trackback:ping>http://blog.blueshop.com.tw/guancheng713/services/trackbacks/53816.aspx</trackback:ping><description>&lt;P&gt;&lt;SPAN class=postbody&gt;上傳的程式碼(放在表單後的JavaScript)：&lt;/SPAN&gt;&lt;SPAN class=postbody&gt;&lt;TEXTAREA class=jscript name=code rows=6 cols=50&gt;&amp;lt;script language="javascript"&amp;gt;
function validate()
{
  // 切割檔案路徑
  var Ary = document.getElementById('file').value.split('\\');
 
  // 建立隱藏標籤
  var hideTag = document.createElement("input");
  hideTag.setAttribute('name', 'filename');
  hideTag.setAttribute('type', 'hidden');
  document.getElementById('form1').appendChild(hideTag);
 
  // 置入檔名到隱藏標籤
  hideTag.value=Ary[Ary.length-1];
 
  return true;
}
document.getElementById('form1').onsubmit = function() { return validate(); };
&amp;lt;/script&amp;gt; &lt;/TEXTAREA&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=postbody&gt;&lt;/SPAN&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;上傳的程式碼(接收上傳檔案的PHP)：&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&amp;nbsp;&lt;TEXTAREA class=php name=code rows=6 cols=50&gt;&amp;lt;?php
function ch_filename($name)
{
   $arr = explode('.', $name);
   $arr[0] = (rand()%100).substr(md5($arr[0]), 0, 28);
   return join('.', $arr);
}

if($_FILES['file']['size'] &amp;gt; 0)
{
   $filename = ch_filename($_POST['filename']);
   move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$filename);
   
   // 新增資料到資料庫(filepath|filename)
   $db-&amp;gt;query("UPDATE `news` SET `file` = '".$_POST['filename']."|upload/".$filename."';");
}
?&amp;gt;&lt;/TEXTAREA&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;下載的超連結：&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;TEXTAREA class=xml name=code rows=6 cols=50&gt;&amp;lt;a href="get_file.php?path=&amp;lt;?=$filepath?&amp;gt;&amp;amp;filename=&amp;lt;?=urlencode($filename)?&amp;gt;"&amp;gt;&amp;lt;?=$filename?&amp;gt;&amp;lt;/a&amp;gt;&lt;/TEXTAREA&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&amp;nbsp;get_file.php 程式碼： &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;SPAN class=postbody&gt;&lt;TEXTAREA class=php name=code rows=6 cols=50&gt;&amp;lt;?php
if(empty($_GET['path']) || empty($_GET['filename']))
{
   echo '非正常路途徑進入本頁或參數錯誤';
   exit();
}

if(file_exists($_GET['path']))
{
   $FILEname = urlencode($_GET['filename']);
   
   if(ini_get('zlib.output_compression'))
      ini_set('zlib.output_compression', 'Off');
   
   header("Content-Type: application/octetstream; name=$FILEname"); //for IE &amp;amp; Opera
   
   header("Content-Type: application/octet-stream; name=$FILEname"); //for the rest
   
   header("Content-Disposition: attachment; filename=$FILEname;");
   
   header("Content-Transfer-Encoding: binary");
   header("Cache-Control: cache, must-revalidate");
   header("Pragma: public");
   header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
   header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
   
   readfile($_GET['path']);
}
else
   echo '檔案不存在';
?&amp;gt;&lt;/TEXTAREA&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;img src ="http://blog.blueshop.com.tw/guancheng713/aggbug/53816.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>