thinkphp3.1 多文件上传图片和文档,怎么保存在不同的文件夹?
thinkphp3.1 上传图片和文档,怎么分开保存?
回复讨论(解决方案)
根据文件后缀,判断上传目录选择
根据文件后缀,判断上传目录选择
$upload->savePath= './Public/image/home/news/';
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
这个上传目录不是只能设置一个么,怎么设置两个,thinkphp上传类第一次用。
thinkphp3.1 没有提供相应的方案,所以需要你自己设计
你可以继承 UploadFile 类,重写 save 方法
也可以用自己的上传类完成
如果你可以获取文件后缀$type
if ($type == '.jpg'){
$upload->savePath= './Public/image/home/ new1/';
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
}else if ($type == '.txt'){
$upload->savePath= './Public/image/home /new2/';
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
}
如果我上传的是一个图片一个文档 2 个个文件呢?
如果你可以获取文件后缀$type
if ($type == '.jpg'){
$upload->savePath= './Public/image/home/ new1/';
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
}else if ($type == '.txt'){
$upload->savePath= './Public/image/home /new2/';
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
}
如果同时上传多个文件,应该会有多个file文件域吧?
这就对file判断不就是?
如果你愿意修改 UploadFile.class.php 文件的话,事情还是有转机的
把 getSaveName 方法中的(408行附近)
$saveName = $rule().".".$filename['extension'];
改成
$saveName = $rule($filename).".".$filename['extension'];
然后
$upload->savePath = '';$upload->saveRule = 'myFunction';
定义函数
function myFunction($filename) { $p = in_array($filename['extension'], array('gif', 'jpg', 'png')) ? '图片路径' : '文档路径'; return $p . $filename['name'];}
由于之后还会加上后缀,所以 $filename['name'] 怎么变形你可随意
如果你愿意修改 UploadFile.class.php 文件的话,事情还是有转机的
把 getSaveName 方法中的(408行附近)
$saveName = $rule().".".$filename['extension'];
改成
$saveName = $rule($filename).".".$filename['extension'];
然后
$upload->savePath = '';$upload->saveRule = 'myFunction';
定义函数
function myFunction($filename) { $p = in_array($filename['extension'], array('gif', 'jpg', 'png')) ? '图片路径' : '文档路径'; return $p . $filename['name'];}
由于之后还会加上后缀,所以 $filename['name'] 怎么变形你可随意
import('ORG.Net.UploadFile');
$upload = new UploadFile();// 实例化上传类
$upload->maxSize = 3145728;
$upload->savePath = '';
$upload->saveRule = 'myFunction';
if(!$upload->upload()) {// 上传错误提示错误信息
$this->error($upload->getErrorMsg());
}else{// 上传成功 获取上传文件信息
$info = $upload->getUploadFileInfo();
}
private function getSaveName($filename) {
$rule = $this->saveRule;
if(empty($rule)) {//没有定义命名规则,则保持文件名不变
$saveName = $filename['name'];
}else {
if(function_exists($rule)) {
//使用函数生成一个唯一文件标识号
$saveName = $rule($filename).".".$filename['extension'];
}else {
//使用给定的文件名作为标识号
$saveName = $rule.".".$filename['extension'];
}
}
if($this->autoSub) {
// 使用子目录保存文件
$filename['savename'] = $saveName;
$saveName = $this->getSubName($filename).$saveName;
}
return $saveName;
}
function my_filename() {
return date('ymdHis',time()).'_'.mt_rand();
}
function myFunction($filename) {
$p = in_array($filename['extension'], array('gif', 'jpg', 'png')) ? './Public/image/home/img/' : './Public/image/home/file/';
return $p . $filename[my_filename()];
}
改了,这三个地方,提示上传路径不存在。
是空的,我在上传目录不存在上一句输出了下。
既然你的 ./Public/image/home/ 是公共的,那么就
$upload->savePath = './Public/image/home/';
函数里只留 'img/' : 'file/'
既然你的 ./Public/image/home/ 是公共的,那么就
$upload->savePath = './Public/image/home/';
函数里只留 'img/' : 'file/'
版主大大,路径可以用了,不过现在是名字是空的了。
文件已经存在!./Public/image/home/img/.jpg
return $p . $filename[my_filename()];
应为
return $p . my_filename();
可直接写作
return $p . date('ymdHis_').mt_rand();
return $p . $filename[my_filename()];
应为
return $p . my_filename();
可直接写作
return $p . date('ymdHis_').mt_rand();

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the "Comment" button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a "picture" button or a "+" button, click

When you find that one or more items in your sync folder do not match the error message in Outlook, it may be because you updated or canceled meeting items. In this case, you will see an error message saying that your local version of the data conflicts with the remote copy. This situation usually happens in Outlook desktop application. One or more items in the folder you synced do not match. To resolve the conflict, open the projects and try the operation again. Fix One or more items in synced folders do not match Outlook error In Outlook desktop version, you may encounter issues when local calendar items conflict with the server copy. Fortunately, though, there are some simple ways to help

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

Apple's recent iPhones capture memories with crisp detail, saturation and brightness. But sometimes, you may encounter some issues that may cause the image to look less clear. While autofocus on iPhone cameras has come a long way, allowing you to take photos quickly, the camera can mistakenly focus on the wrong subject in certain situations, making the photo blurry in unwanted areas. If your photos on your iPhone look out of focus or lack sharpness overall, the following post should help you make them sharper. How to Make Pictures Clearer on iPhone [6 Methods] You can try using the native Photos app to clean up your photos. If you want more features and options

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

It is 395 words, which is 495. This article will show you how to add red lines in Word documents. Redlining a document refers to making modifications to the document so that users can clearly see the changes. This feature is very important when multiple people are editing a document together. What redline means Marking a document Redlining means using red lines or callouts to indicate changes, edits, or revisions to a document. The term was inspired by the practice of using a red pen to mark printed documents. Redline comments are widely used in different scenarios, such as clearly showing recommended changes to authors, editors, and reviewers when editing a document. Propose changes and modifications in legal agreements or contracts Provide constructive criticism and suggestions on papers, presentations, etc. How to give W

The Windows folder contains the Windows operating system and is an important folder in a Windows computer. By default, Windows is installed on the C drive. Therefore, C is the default directory for Windows folders. Every Windows computer has a Windows folder. However, some users reported that two Windows folders were found in the C drive. In this article, we will explain what you can do if you encounter such a situation. Two Windows folders in C drive It is rare to have two Windows folders in C drive. However, if you encounter such a situation, you can use the following suggestions: Run an anti-malware scan to try to find the correct
