基于PHP的AJAX技术实现文件异步上传

源代码

<?php
$upload_dir = "/var/www/anyexample/aeu"; // 文件存储的路径
$web_upload_dir = "/aeu"; // 文件在Web目录下的路径
$tf = $upload_dir."/".md5(rand()).".test";
$f = @fopen($tf, "w");
if ($f == false)
die("Fatal error! {$upload_dir} is not writable. Set "chmod 777 {$upload_dir}"
or something like this");
fclose($f);
unlink($tf);

//处理上传的文件
if (isset($_POST["fileframe"]))
{
 $result = "ERROR";
 $result_msg = "No FILE field found";

 if (isset($_FILES["file"])) // 从浏览器接受文件
 {
  if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) // 没有错误
  {
   $filename = $_FILES["file"]["name"]; // 文件名 数据挖掘研究院
   move_uploaded_file($_FILES["file"]["tmp_name"], $upload_dir."/".$filename);
   // 处理的主过程-转移文件到 $upload_dir
   $result = "OK";
  }
  elseif ($_FILES["file"]["error"] == UPLOAD_ERR_INI_SIZE)
   $result_msg = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
  else
   $result_msg = "Unknown error";
 }

 echo "<html><head><title>-</title></head><body>";
 echo "<script language="JavaScript" type="text/javascript">"." ";
 echo "var parDoc = window.parent.document;";
 "
 if ($result == "OK")
 {
  echo "parDoc.getElementById("upload_status").value = "file successfully uploaded";";
  echo "parDoc.getElementById("filename").value = "".$filename."";";
  echo "parDoc.getElementById("filenamei").value = "".$filename."";";
  echo "parDoc.getElementById("upload_button").disabled = false;";
 }
 else
 {
  echo "parDoc.getElementById("upload_status").value = "ERROR: ".$result_msg."";";
 }

 echo " "."</script></body></html>";
 exit();
}

function safehtml($s)
{
 $s=str_replace("&", "&", $s);
 $s=str_replace("<", "<", $s);
 $s=str_replace(">", ">", $s);
 $s=str_replace(""", "'", $s);
 $s=str_replace(""", """, $s);
 return $s;
}

if (isset($_POST["description"]))
{
 $filename = $_POST["filename"];
 $size = filesize($upload_dir."/".$filename); 数据挖掘工具
 $date = date("r", filemtime($upload_dir."/".$filename));
 $description = safehtml($_POST["description"]);

 $html =<<<END
 <html><head><title>{$filename} [uploaded by IFRAME Async file uploader]</title></head>
 <body>
  <h1>{$filename}</h1>
  <p>This is a file information page for your uploaded file. Bookmark it, or send to anyone...</p>
  <p>Date: {$date}</p>
  <p>Size: {$size} bytes</p>
  <p>Description:
  <pre>{$description}</pre>
  </p>
  <p><a href="{$web_upload_dir}/{$filename}" style="font-size: large;">download file</a><br>
  <a href="{$PHP_SELF}" style="font-size: small;">back to file uploading</a><br>
  <a href="{$web_upload_dir}/upload-log.html" style="font-size: small;">upload-log</a></p>
  <br><br>Example by <a href="http://www.anyexample.com/">AnyExample</a> 数据挖掘实验室
 </body></html>
 END;
 
 $f = fopen($upload_dir."/".$filename."-desc.html", "w");
 fwrite($f, $html);
 fclose($f);
 $msg = "File {$filename} uploaded,
 <a href="{$web_upload_dir}/{$filename}-desc.html">see file information page</a>";

 $f = fopen($upload_dir."/upload-log.html", "a");
 fwrite($f, "<p>$msg</p> ");
 fclose($f);

 setcookie("msg", $msg);
 header("Location: http://".$_SERVER["HTTP_HOST"].$PHP_SELF);
 exit();
}

if (isset($_COOKIE["msg"]) && $_COOKIE["msg"] != "")
{
 if (get_magic_quotes_gpc())
  $msg = stripslashes($_COOKIE["msg"]);
 else
  $msg = $_COOKIE["msg"];
  setcookie("msg", "");
}
?>
<!-- Beginning of main page -->
<html><head> 数据挖掘交友
<title>IFRAME Async file uploader example</title>
</head>
<body>
<?php
 if (isset($msg))
  echo "<p style="font-weight: bold;">".$msg."</p>";
?>
<h1>Upload file:</h1>
<p>File will begin to upload just after selection. </p>
<p>You may write file description, while you file is being uploaded.</p>

<form action="<?=$PHP_SELF?>" target="upload_iframe" method="post" enctype="multipart/form-data">
 <input type="hidden" name="fileframe" value="true">
 <!-- Target of the form is set to hidden iframe -->
 <!-- From will send its post data to fileframe section of this PHP script (see above) -->

 <label for="file">text file uploader:</label><br>
 <!-- JavaScript is called by OnChange attribute -->
 <input type="file" name="file" id="file" onChange="jsUpload(this)">

数据挖掘论坛


</form>
<script type="text/javascript">
/* This function is called when user selects file in file dialog */
function jsUpload(upload_field)
{
 // this is just an example of checking file extensions
 // if you do not need extension checking, remove
 // everything down to line
 // upload_field.form.submit();
 
 var re_text = /.txt|.xml|.zip/i;
 var filename = upload_field.value;

 /* Checking file type */
 if (filename.search(re_text) == -1)
 {
  alert("File does not have text(txt, xml, zip) extension");
  upload_field.form.reset();
  return false;
 }

 upload_field.form.submit();
 document.getElementById("upload_status").value = "uploading file...";
 upload_field.disabled = true;
 return true;
}
</script>
<iframe name="upload_iframe" style="width: 400px; height: 100px; display: none;"> 数据挖掘论坛
</iframe>
<!-- For debugging purposes, it"s often useful to remove
"display: none" from style="" attribute -->

<br>
Upload status:<br>
<input type="text" name="upload_status" id="upload_status"
value="not uploaded" size="64" disabled>
<br><br>

File name:<br>
<input type="text" name="filenamei" id="filenamei" value="none" disabled>

<form action="<?=$PHP_SELF?>" method="POST">
 <!-- one field is "disabled" for displaying-only. Other, hidden one is for sending data -->
 <input type="hidden" name="filename" id="filename">
 <br><br>

 <label for="photo">File description:</label><br>
 <textarea rows="5" cols="50" name="description"></textarea>

 <br><br> 数据挖掘研究院
 <input type="submit" id="upload_button" value="save file" disabled>
</form>
<br><br>
<a href="<?=$web_upload_dir?>/upload-log.html">upload-log</a>
<br><br><br>

Example by <a href="http://www.anyexample.com/">AnyExample</a>
</body>
</html>

  以上的讲解就是提供一种思路供大家参考。大家也可以根据自己的需求进行相应的优化。

数据挖掘论坛

[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:在PHP中全面阻止SQL注入式攻击之一
下一篇:基于PHP的AJAX技术实现文件异步上传
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • P2P Search Engines-Napster
  • Peer to Peer ( P2P ) 综述
  • P2P Search Engines-Introduction
  • P2P Routing
  • P2P Search Engines-Music and P2P
  • P2P Search Engines-Historical Developmen
  • P2P Security
  • 如何从一个php文件向另一个地址post数据,
  • 基于PHP的AJAX技术实现文件异步上传
  • Copyright and P2P
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • P2P Search Engines-Introduction
  • P2P Search Engines-Music and P2P
  • P2P Search Engines-Historical Developmen
  • Copyright and P2P
  • P2P Search Engines-Napster
  • GNUtella
  • YouServ ?An Analysis
  • Freenet
  • P2P Search Engines
  • P2P Routing
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静