Home > Backend Development > PHP Tutorial > PHP文件下传

PHP文件下传

WBOY
Release: 2016-06-13 13:25:37
Original
1123 people have browsed it

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
?
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template