


PHP solution to the problem of the same name when uploading images or files in batches in TP3.2
The example in this article shares with you how to solve the problem of name conflict when uploading files or pictures in batches in TP3.2 for your reference. The specific content is as follows
1, html
<form action="{:U('Upload/index')}" enctype="multipart/form-data" method="post" > <p><input type="file" id="file3" name="ID[]" /></p> <p><input type="file" id="file4" name="ID[]" /></p> <input type="submit" value="上传" /> <p><img id="img1" alt="" src="/Public/IMAGE/empty_thumb.gif" /></p> </form>
2, php
public function index(){ if(!empty($_FILES)){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728; $upload->rootPath = './Uploads/'; $upload->savePath = 'image/'; //$upload->saveName = date('YmdHis').'-'.randomkeys(3);//msectime(),毫秒数13位 $upload->saveName = 'msectime'; //自定义函数,采用13位毫秒和3位随机数 $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); $upload->autoSub = true; $upload->subName = array('date','Ymd'); /* 判断$_FILES[$key]是否:一维数组,单张图片上传 -xzz0703 * 原理:html的input type = "file" name="IDcard"和name="IDcard[]"的区别: * $_FILES前者到后台php是二维数组,后者是三维数组 */ foreach($_FILES as $key=>$value){ if(count($_FILES[$key]) == count($_FILES[$key],1)){ $info = $upload->uploadOne($_FILES[$key]); if($info){ echo json_encode(array('code'=>200,'id'=>$img_id,'name'=>$img_name));exit; }else{ echo json_encode(array('code'=>0,'msg'=>$upload->getError()));exit; } } } if(count($_FILES)){ $info = $upload->upload();//如果是二维数组,使用批量上传文件的方法 if(!$info){ $this->error($upload->getError()); exit; } $img_url = '/Uploads/'.$info[0]['savepath'].$info[0]['savename']; $res = array('imgPath1'=>$img_url,code=>$img_url,'msg'=>$info); echo json_encode($res); } } }
3. Core: Many friends are stuck on the saveName attribute when using the TP3.2 framework. The reason is that the upload server processing level takes millions of microseconds. soon.
Solution: saveName = 13 digits of milliseconds + 3 digits of random number, perfect solution, specific code:
//返回当前的毫秒时间戳和随机数合并的字符串 function msectime() { list($msec, $sec) = explode(' ', microtime()); $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000).randomkeys(3); return $msectime; }
The above is the detailed content of PHP solution to the problem of the same name when uploading images or files in batches in TP3.2. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
