Home > php教程 > php手册 > body text

文件上传实例: 无刷新 & 重命名 & 回调

WBOY
Release: 2016-06-06 20:09:17
Original
1125 people have browsed it

重命名 : 为防止文件名重复, 使用的规则是 年月日时分秒 + 随机数序 Ajax callback : use jquery.form.js html+js: htmlbody!--表单--form action="upload_file.php" method="post" enctype="multipart/form-data" id="xhdform"input type="file" name="file

  • 重命名 : 为防止文件名重复, 使用的规则是 年月日时分秒 + 随机数序
  • Ajax & callback : use jquery.form.js

html+js:


<!--表单-->
Copy after login

upload_file.php:

<?php $folder = 'upload/';
if (! file_exists ( $folder )) {mkdir ( $folder );}//新增: create folder "upload"
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png")) // 允许上传的文件类型
&& ($_FILES["file"]["size"] < 20000)) // max size
if( preg_match( "#^image/((?:gif)|(?:jpg)|(?:jpeg)|(?:png))$#is", $_FILES["file"]["type"], $match ) && ($_FILES["file"]["size"] < 2097152) )
// 20000差不多是20kb太小了, 刚才修改了一下, 改成2M; 另外此行preg_match和上面["file"]["type"]==的效果一样
  {
  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"])) // if file already exists
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
		// rename rule begin ~~~~ xiaohudie
		for ( $i=0; $i
Copy after login

代码写多了感觉自己中文跟不上节奏了, 专注装逼十九年其实我想用全英文写注释的

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!