Home Backend Development PHP Tutorial PHP iframe implements refresh-free file upload (no ajax required)_PHP tutorial

PHP iframe implements refresh-free file upload (no ajax required)_PHP tutorial

Jul 20, 2016 am 11:10 AM
ajax iframe php one time upload introduce about refresh accomplish document article method of

This article will introduce the method of uploading files. One method uses php iframe to achieve uploading without refreshing. Students in need can refer to it.

First of all, ajax cannot upload files, which misled me for a while. I couldn’t sleep tonight, so I followed the instructions and uploaded files without refreshing

In fact, the principle is very simple

The code is as follows Copy code td>
 代码如下 复制代码


         
         

   
         

Compared with the general
tag, it only has one more target attribute, which is used to specify the label page. Where to open and submit data.

If this attribute is not set, the URL in the action will be redirected and opened on this page as usual.
 代码如下 复制代码

class upload
{
   public $_file;

   public function __construct( $name =null)
   {
       if(is_null($name) || !isset($_FILES[$name]))
            $name = key($_FILES);
           
       if(!isset($_FILES[$name]))
           throw new Exception("并没有文件上传");
          
       $this->_file  = $_FILES[$name];
      
       if(!is_uploaded_file($this->_file['tmp_name']))
            throw new Exception("异常情况");
       if($this->_file['error'] !== 0)
            throw new Exception("错误代码:".$this->_file['error']);    
   }
   public function moveTo( $new_dir)
   {
       $real_dir = $this->checkDir($new_dir);
       return move_uploaded_file($this->_file['tmp_name'], $real_dir.'/'.$this->_file['name']);
   }
   private function checkDir($dir)
   {
       $real_dir = realpath($dir);
       if($real_dir === false)
           throw new Exception("给定目录{$dir}不存在");
       if(!is_writable($real_dir))
           throw new Exception("给定目录{$dir}不可写");
       return $real_dir;
   }

}

And if it is set to the name value of the iframe, that is, "upload", it will be opened in the iframe. Because the CSS is set to hidden, there will be no movement. If you remove display:none, you will also see the return information from the server. Also post the classes you organized.
The code is as follows Copy code
class upload{ public $_file; public function __construct( $name =null) { if(is_null($ name) || !isset($_FILES[$name])) $name = key($_FILES); if(!isset($_FILES[$name])) throw new Exception("No file uploaded");                                                                                                                                                                          tmp_name']))                                                                                                                            thanier ".$this->_file['error']); "." }" public function moveTo( $new_dir) " return move_uploaded_file($this->_file['tmp_name'], $real_dir.'/'.$this->_file['name']); } private function checkDir($ dir) { $real_dir = realpath($dir); if($real_dir === false) throw new Exception("The given directory {$dir} does not exist"); if(!is_writable($real_dir)) throw new Exception("The given directory {$dir} is not writable"); return $real_dir; }}

Call example:

The code is as follows
 代码如下 复制代码

$inputName =  'uploadfile';
 // 即 中的name值,不填也行
$upload = new upload($inputName);
$new_dir = "/www";  // 将文件移动到的路径
$upload->moveTo($new_dir);

Copy code
$inputName = 'uploadfile';
// That is, the name value in is left blank. Also OK
$upload = new upload($inputName);
$new_dir = "/www"; // The path to move the file to
$upload->moveTo($new_dir) ;

http://www.bkjia.com/PHPjc/444701.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/444701.html
TechArticle
This article will introduce the method of file upload. One uses php iframe to achieve upload. This is also without refresh. Oh, students in need can refer to it. First of all, ajax does not...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles