search
Homephp教程php手册在PHP中实现文件的上传功能

在PHP中实现文件的上传功能

Jun 13, 2016 am 10:30 AM
phpuploadmainstreamFunctionexistaccomplishdevelopsupportdocumentnowoflanguage


  php(做为现在的主流开发语言)同样支持上传文件的功能,但并不是所有支持php(做为现在的主流开发语言)的网站都支持此功能,特别是免费的网站。
  
  实现上传,首先要在HTML上加入上传文件的“

  
  
      
  用php(做为现在的主流开发语言)上传时,需要对内容作详细的检查,例如是否容许读写文件,文件格式、文件大小是否在你指定的大小内等。
  
  
  $file_size_max = 1000000;
  // 限制文件上传最大容量(bytes)
  $store_dir = "/public/www/upload/";
  // 上传文件的储存位置
  $accept_overwrite = true;
  //允许读写文件
  // 检查文件大小
  if ($upload_file_size > $file_size_max) {
  echo "对不起,你的文件容量大于规定";
  exit;
  }
  // 检查读写文件
  if (file_exists($store_dir . $upload_file_name) &&&& !$accept_overwrite) {
  echo "文件已存在,不能再复制";
  exit;
  }
  //复制文件到指定目录
  if (! @ copy($upload_file,$store_dir . $upload_file_name)) {
  echo "复制文件失败";
  exit;
  }
  echo "上传文件完成";
  ?>
  
  需要注意的是,php(做为现在的主流开发语言)在上传文件的时候是将文件复制到服务器临时目录(temp),然后利用php(做为现在的主流开发语言)的“copy()”函数将文件由临时目录复制到你指定的储存目录。由于程序会使用临时目录来进行工作,如果服务器因考虑安全问题而封锁了以上的功能,你就无法使用 php(做为现在的主流开发语言)的上传功能。
  
  另外,上传的文件目录亦需要设定文件模式为777(CHMOD 777) ,否则php(做为现在的主流开发语言)就无权读写文件。
Statement
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 Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor