Home > Backend Development > PHP Tutorial > Examples of php file upload (normal upload and asynchronous upload)

Examples of php file upload (normal upload and asynchronous upload)

WBOY
Release: 2016-07-25 08:56:45
Original
1424 people have browsed it

复制代码

2,upload_file.php

  1. //php普通文件上传
  2. //by bbs.it-home.org
  3. if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)){
  4. if ($_FILES["file"]["error"] > 0) {
  5. echo "Return Code: " . $_FILES["file"]["error"] . "
    ";
  6. }else {
  7. echo "Upload: " . $_FILES["file"]["name"] . "
    ";
  8. echo "Type: " . $_FILES["file"]["type"] . "
    ";
  9. echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
    ";
  10. echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
    ";
  11. if (file_exists("upload/" . $_FILES["file"]["name"])){
  12. echo $_FILES["file"]["name"] . " already exists. ";
  13. }else{
  14. move_uploaded_file($_FILES["file"]["tmp_name"],
  15. "upload/" . $_FILES["file"]["name"]);
  16. echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  17. }
  18. }
  19. }else {
  20. echo "Invalid file";
  21. }
  22. ?>
复制代码

二、异步文件上传 使用iframe异步上传文件。 1,前端html

  1. 导入文件:
复制代码

2,Js代码

  1. function startUpload() {
  2. var spanObj = document.getElementById("info");
  3. spanObj.innerHTML = " 开始上传";
  4. document.getElementById("upForm").sumbit();
  5. }
  6. //回调
  7. function stopUpload(responseText){
  8. var spanObj = document.getElementById("info");
  9. spanObj.innerHTML = "上传成功";
  10. spanObj.innerHTML = responseText;
  11. }
复制代码

2), server-side code

  1. $file = $_FILES['myfile'];

  2. $fileName = uploadFile($file);
  3. //$result = readFromFile("../upload /" . $fileName);
  4. echo "";
  5. function uploadFile($ file) {
  6. // Upload path
  7. $destinationPath = "../upload/";
  8. if (!file_exists($destinationPath)){
  9. mkdir($destinationPath , 0777);
  10. }
  11. //Rename
  12. $fileName = date('YmdHis') . '_' . iconv('utf-8' , 'gb2312' , basename($file['name']));
  13. if (move_uploaded_file($file['tmp_name'], $ destinationPath . $fileName)) {
  14. return iconv('gb2312' , 'utf-8' , $fileName);
  15. }
  16. return '';
  17. }

  18. //Code Comments

  19. / *
  20. 1, about the basename method
  21. $path = "/testweb/home.php";
  22. //Display the file name with the file extension
  23. echo basename($path);
  24. //Display without the file extension The file name
  25. echo basename($path,".php");
  26. 2, about iconv
  27. iconv('gb2312', 'utf-8', $fileName);//Convert $fileName from gb2312 to utf- 8 format.
  28. Note: This function needs to open php_iconv.dll in php.ini
  29. 3, about $_FILES['myfile']
  30. $_FILES is equivalent to a two-dimensional array, and $_FILES['myfile'] is equivalent to a one-dimensional array array. So
  31. $f = $_FILES['myfile'];
  32. echo $f['name'];
  33. If you directly access the $_FILES['myfile'], Undefined index: myfile will be reported. At this time add
  34. if(!isset($_FILES['myfile'])){
  35. die('The uploaded file does not exist!');
  36. }
  37. */

Copy code


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