PHP文件下传

Jun 13, 2016 am 10:29 AM
file files name

PHP文件上传

一、普通文件上传方式

使用原生态的php上传文件。

(1)前端代码

    
Copy after login
Copy after login

?

(2)php代码(upload_file.php)

    <?php if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)){        if ($_FILES["file"]["error"] > 0) {          echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";                  if (file_exists("upload/" . $_FILES["file"]["name"])){               echo $_FILES["file"]["name"] . " already exists. ";            }else{               move_uploaded_file($_FILES["file"]["tmp_name"],               "upload/" . $_FILES["file"]["name"]);               echo "Stored in: " . "upload/" . $_FILES["file"]["name"];            }        }            }else {        echo "Invalid file";      }      ?>  
Copy after login
?

?

?

二、异步文件上传方式

目的是尽量少使用插件。于是使用iframe异步上传文件

?

(1)前端html

    
Copy after login
Copy after login
导入文件:

?

    function startUpload() {          var spanObj = document.getElementById("info");          spanObj.innerHTML = " 开始上传";          document.getElementById("upForm").sumbit();      }      //回调      function stopUpload(responseText){          var spanObj = document.getElementById("info");          spanObj.innerHTML = "上传成功";          spanObj.innerHTML = responseText;      }  
Copy after login

?

(2)服务器端代码

    $file = $_FILES['myfile'];      $fileName = uploadFile($file);      //$result = readFromFile("../upload/" . $fileName);      echo "<script type="text/javascript">window.top.window.stopUpload('{$fileName}')</script>";            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 '';      }  
Copy after login

?

    //代码注释      /*     1,关于basename方法     $path = "/testweb/home.php";     //显示带有文件扩展名的文件名     echo basename($path);     //显示不带有文件扩展名的文件名     echo basename($path,".php");          2,关于iconv     iconv('gb2312' , 'utf-8' , $fileName);//将$fileName从gb2312转为utf-8格式。     注:该函数需要开启php.ini里面的php_iconv.dll          3,关于$_FILES['myfile']     $_FILES相当于一个二维数组,而$_FILES['myfile']相当于一个一维数组。所以可以     $f = $_FILES['myfile'];     echo $f['name'];          如果直接访问该$_FILES['myfile'],则会报Undefined index: myfile。此时加上     if(!isset($_FILES['myfile'])){         die('上传文件不存在!');     }     */  
Copy after login
?
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Use java's File.length() function to get the size of the file Use java's File.length() function to get the size of the file Jul 24, 2023 am 08:36 AM

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file How to convert php blob to file Mar 16, 2023 am 10:47 AM

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Rename files using java's File.renameTo() function Rename files using java's File.renameTo() function Jul 25, 2023 pm 03:45 PM

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParent() function to get the parent path of the file Use java's File.getParent() function to get the parent path of the file Jul 24, 2023 pm 01:40 PM

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of the file Use java's File.getParentFile() function to get the parent directory of the file Jul 27, 2023 am 11:45 AM

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

How to delete a file or directory using File.delete() method in Java? How to delete a file or directory using File.delete() method in Java? Nov 18, 2023 am 08:02 AM

How to delete a file or directory using File.delete() method in Java? Overview: In Java, we can delete a file or directory using the delete() method of the File class. This method is used to delete the specified file or directory. However, it should be noted that this method can only delete empty directories or files that are not opened by other programs. If file or directory deletion fails, you can find the specific reason by catching IOException. Step 1: Import related packages First, we need

Create multi-level directories using java's File.mkdirs() function Create multi-level directories using java's File.mkdirs() function Jul 24, 2023 am 11:04 AM

Create multi-level directories using Java's File.mkdirs() function In Java, we often need to create folders to store and organize files. Sometimes, we need to create multi-level directories, which are folders containing subfolders. Java provides the mkdirs() function of the File class to implement this function. The File class is a class in Java that handles files and directories. It provides a series of methods for operating files and directories. Among them, the mkdirs() function is a function that creates multi-level directories. Down

See all articles