Home > php教程 > php手册 > body text

php无刷新利用iframe实现页面无刷新上传文件(1/2)

WBOY
Release: 2016-06-13 11:25:23
Original
1643 people have browsed it

利用form表单的target属性和iframe

一、上传文件的一个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代码

  

 
导入文件:
   

1 2

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!