首页 > 编程技术 > php

php iframe 无刷新文件上传代码

发布时间:2016-11-25 17:32

其它原理很简单,利用form表单的target属性和iframe来实现的,打开为iframe试就行了,返回就利用js判断php教程运行后返回的参数是不是成功
一、上传文件的一个php方法。
该方法接受一个$file参数,该参数为从客户端获取的$_files变量,返回重新命名后的文件名,如果上传失败,则返回空字符串。

php代码

    function uploadfile($file) {
        // 上传路径     $destinationpath = "./upload/";
        if (!file_exists($destinationpath)){
            mkdir($destinationpath , 0777);     }
        //重命名
        $filename = date('ymdhis') . '_' . iconv('utf-8' , 'gb2312' , basename($file['name']));
        if (move_uploaded_file($file['tmp_name'], $destinationpath . $filename)) {         return iconv('gb2312' , 'utf-8' , $filename);
        }     return '';
      }

二、客户端html代码
这里正是技巧所在,添加另一个iframe来实现。表单标签form定义了一个属性target,该属性解释如下:
[pre]target属性:
_blank   ----------   新开窗口
_self   -----------   自身
_top   ------------   主框架
_parent   ---------   父框架
自定义名字     -----   出现于框架结构,将会在该名称的框架内打开链接

本例中采用iframe名字,所以表单在提交时会在iframe内打开链接(即无刷新,确切的说应该是
感觉无刷新)
在表单提交时,调用startupload方法,当然这是js定义的。
[/pre][pre]此外我们还定义一个span来显示提示信息。代码如下:
[/pre]xhtml代码

   

<form id="upform" action="upload.php" method="post" enctype="multipart/form-data" target="upload_target" onsubmit="startupload()"> 导入文件:<input type="file" name="myfile" id="myfile" />
    <input type="submit" name="submitbtn" value="导入" /> <iframe id="upload_target" name="upload_target"" width=100% src="#" style="width:0;height:0;border:0px solid #fff;">iframe>
    form> <span id="info">span>

三、js部分

这部分比较简单,只是显示提示信息

网页特效代码
    function startupload() {
        var spanobj = document.getelementbyid("info");     spanobj.innerhtml = " 开始上传";
    }
    function stopupload(responsetext){     var spanobj = document.getelementbyid("info");
        spanobj.innerhtml = " 上传成功;     spanobj.innerhtml = responsetext;
      }

接下来就要看服务器端得处理了。

四、服务器段处理部分

php代码

    $file = $_files['myfile'];   $filename = uploadfile($file);
      $result = readfromfile("./upload/" . $filename);

此外在后面还应该加上一句js代码用来调用stopupload方法。
javascript代码

  

    window.top.window.stopupload("");

最后在补上php中的readfromfile方法,就大功告成了。
php代码

    function readfromfile($target_path) {
        // 读取文件内容     $file = fopen($target_path,'r') or die("unable to open file");
        $filecontent = '';     while(!feof($file))
        {         $str = fgets($file);
            $filecontent .= $str;     }
        fclose($file);     return $filecontent;
      }

总结:方法很简单扼要,如果你以前没想到觉得ajax无刷新文件上传很难,现在明白利用iframe target来制作就觉得很容易了。

<?
function turntopage($url="index.php",$info = "页面转向中...",$second=2){
print "<html>n<head>n<title>页面转向中....</title>n";
print "<meta http-equiv="refresh" content="$second;url=$url">n"; //开源代码111cn.net
print "<style type="text/css教程">n<!--n";
print "td { font-family: "verdana", "arial";font-size: 12px}n";
print "a {color: #000000; text-decoration: none}n";
print "-->n</style>n";
print "</head>n<body>n";
print "<table width="100%" border="0" align="center">n";

//111cn.net

print " <tr>n";
print " <td height="200"> </td>n";
print " </tr>n";
print " <tr>n";
print " <td align="center">n";
print " <table width="60%" border="0" cellpadding="8" bgcolor="#aa9fff">n";

//开源代码111cn.net

print " <tr>n";
print " <td height="30" align="center">页面转向提示信息</td>n";
print " </tr>n";
print " <tr>n";
print " <td align="center">$info</td>n";
print " </tr>n";
print " <tr>n";

//111cn.net

print " <td align="center">n";
print " <a href="$url">如果你的浏览器不支持自动跳转,请按这里</a></td>n";
print " </tr>n";
print " </tr>n";
print " </table></td>n";
print " </tr>n";
print " <tr>n"; //开源111cn.net
print " <td height="200"> </td>n";
print " </tr>n";
print "</table>n";
print "</body>n</html>";
exit;
?>

php重定向
<?
方法一:header("location: index.php");
方法二:echo "<scrīpt>window.location ="$php_self";</scrīpt>";

//开源代码111cn.net

方法三:echo "<meta http-equiv="refresh" content="0; url=index.php">";
?>

 

第一种比较简单就是files利用index.php教程接入值就成了,没经过判断,都会用到move_uploaded_files与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"] - 由文件上传导致的错误代码
这是一种非常简单文件上传方式。基于安全方面的考虑,您应当增加有关什么用户有权上传文件的限制

 

<form action="index.php" method="post"  enctype="multipart/form-data">
      缩略图:<input type="file" name="files" />
      <input type="submit" value="提交"/>
    </form>
  </body>
</html>

php代码

1 <?php2         $temp_name = $_files[files][tmp_name];            //临时文件3          $now_name  = $_files[files][name];                    //当前文件4          move_uploaded_file($temp_name,'./images/'.date('yyyymdhis').$now_name);    //移动到指定的文件夹下面5  ?>

下面来看一个完整一点的文件上传代码

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>

php处理文件

<?php
if ($_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"];
  }
?>

<form> 标签的 enctype 属性规定了在提交表单时要使用哪种内容类型。在表单需要二进制数据时,比如文件内容,请使用 "multipart/form-data"。

<input> 标签的 type="file" 属性规定了应该把输入作为文件来处理。举例来说,当在浏览器中预览时,会看到输入框旁边有一个浏览按钮。

注释:允许用户上传文件是一个巨大的安全风险。请仅仅允许可信的用户执行文件上传操作。

如果你要限制文件上传类型与大小可以这样

if ((($_files["file"]["type"] == "image/gif")
|| ($_files["file"]["type"] == "image/jpeg")
|| ($_files["file"]["type"] == "image/pjpeg"))
&& ($_files["file"]["size"] < 20000))

php教程 url地址 email ip地址 验证
以前验证邮箱,ip,url地址都要用正则,现在我们利用php filter_validate_url,filter_validate_email,filter_validate_ip三个函数。
filter_validate_url 把值作为 url 来验证。
filter_validate_email 把值作为 e-mail 来验证。
filter_validate_ip 把值作为 ip 地址来验证。

*/

$url = "example.php?name=peter&age=37";

if(!filter_var($url, filter_validate_url, filter_flag_query_required))
{
echo "url is not valid";
}
else
{
echo "url is valid";
}

/*
filter_validate_email 过滤器把值作为电子邮件地址来验证
*/

$email = "someone@exa mple.com";

if(!filter_var($email, filter_validate_email))
 {
 echo "e-mail is not valid";
 }
else
 {
 echo "e-mail is valid";
 }

/*
filter_validate_ip 过滤器把值作为 ip 进行验证。
*/
$ip = "192.168.0.1";

if(!filter_var($ip, filter_validate_ip))
 {
 echo "ip is not valid";
 }
else
 {
 echo "ip is valid";
 }
 
 //ipv6
 
 $ip = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";

if(!filter_var($ip, filter_validate_ip, filter_flag_ipv6))
 {
 echo "ip is not valid";
 }
else
 {
 echo "ip is valid";
 }

因为工作上的需要我要对一批数据进行按日期进行更新发布日期,如果我要从今天20110112开始向前更新30天数据,我们发现在利用php教程 strtotime函数与date很容易实现代码哪下。

strtotime() 函数将任何英文文本的日期时间描述解析为 unix 时间戳。

语法
strtotime(time,now)参数 描述
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。


*/

$cu_date = date("ymd");
$mk = strtotime($cu_date);
for( $i=1;$i<=30;$i++)
{
 $mk = $mk -86500;
 echo date("ymd",$mk).'<br />';
}

/*

20110110
20110109
20110108
20110107
20110106
20110105
20110104
20110103
20110102
20110101
20101231
20101230
20101229
20101228
20101227
20101226
20101225
20101224
20101223
20101222
20101221
20101220
20101219
20101218
20101217
20101216
20101215
20101214
20101213
20101212

核心代码就是$mk -86500哦,因为一天是86500毫秒,所以利用上次相减的时间再减86500就是要更新的时间了.

标签:[!--infotagslink--]

您可能感兴趣的文章: