Home Backend Development PHP Tutorial thinkphp implements image upload function sharing_PHP tutorial

thinkphp implements image upload function sharing_PHP tutorial

Jul 13, 2016 am 10:37 AM
thinkphp upload picture

1. We first need to create a table

Copy code The code is as follows:

CREATE TABLE IF NOT EXISTS `tp_image` (
`id` int(11 ) NOT NULL AUTO_INCREMENT,
`image` varchar(200) NOT NULL,
`create_time` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET =utf8;

2. Then add the configuration in the conf file (the last configuration is optional, just for the convenience of unified management of URL paths)

Copy code The code is as follows:

return array(
'URL_MODEL' => ; 2, // If your environment does not support PATHINFO, please set it to 3
'DB_TYPE' => 'mysql',
'DB_HOST' => ; 'thinkphp',
'DB_USER' => 'root',
'DB_PWD' => ',
'DB_PORT' => '3306',
'DB_PREFIX' = > Replace the Upload directory
in the root directory '__UPLOAD__' =>
3. Add an Image module (you can choose any name)




Copy code

The code is as follows:
class ImageAction extends Action{

/**

      * Create index entry method        */ public function index(){ $image=M('Image'); ;find(); //Get the last uploaded image $this->assign('data',$data); ;



4. Create the corresponding index view file (index.html)




Copy code

The code is as follows:





Insert title here



Upload allowed file types: gif png jpg image files, And generate 2 thumbnails, the large image is watermarked, and the original image will be deleted after generation.









5. Select the image and click the upload button, it will jump to the upload method of the Image module. There is no such method on the Image module yet, so we create it

Copy code The code is as follows:

class ImageAction extends Action{

/** <*>*/
Public function index () {
$ Image = m ('Image');
$ data = $ image- & gt; order ('create_time desc') ;find(); //Get the last uploaded image

var_dump($data);
$this->assign('data',$data);
$this->display ();
                                                                                                                                                                                                                                                                                                 if(!empty($_FILES))
                                                                                                                                                                      
6. If the submission is not NULL, jump to the _upload method. This method implements the image upload function




Copy code

The code is as follows:

class ImageAction extends Action{

/**
      * Create index entry method
       */
public function index(){
$image=M(' Image'); ; $ this->assign('data',$data); Jump to _upload method
public function upload(){ $this->_upload( );
}

}

                                                        >                   $upload = new UploadFile(); $upload->allowExts = explode (',', 'jpg,gif,png,jpeg');
//Set the attachment upload directory
The thumbnail diagram is effective for image files
$ upload-& gt; thumb = true;
// Set the reference image library package path
$ upload- & gt; imageClasspath = '@.org.image'
                                                                                                                                                                                        $ upload->thumbMaxWidth = '400,100';
🎜> $upload-> ;saveRule = 'uniqid';
Upload failed
if (!$upload ->upload())
                                                                                                                                                                   
else
{
                                                                                                                     >                  import('@.ORG.Image');                                                                                                                                                                                         : :water($uploadList[0]['savepath'] . 'm_' . $uploadList[0]['savename'], APP_PATH.'Tpl/Public/Images/logo.png');

// The picture name is assigned to the field image
$ _post ['Image'] = $ uploadlist [0] ['savename'];
}
$ model = m ('image');
//Save the current data object
$data['image'] = $_POST['image'];
$data['create_time'] = NOW_TIME;
$list = $model->add ($data);
if ($list !== false)
{
$this->success('Uploaded image successfully!');
}
else
{
                                                                                                                                                                                                             ‐
The upload was successful and two thumbnails were generated

What needs to be explained is:

The image upload class (UploadFile.class.php) and image model class (Image.class.php) that come with ThinkPHP require the full version of the ThinkPHP package.

If not, you need to create a folder (ORG) in Lib, then go to the official website to download the expansion package and put these two files into the ORG folder.

Mine is the second situation



http://www.bkjia.com/PHPjc/736807.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/736807.html

TechArticle

1. We first need to create a table and copy the code as follows: CREATE TABLE IF NOT EXISTS `tp_image` ( `id ` int(11) NOT NULL AUTO_INCREMENT, `image` varchar(200) NOT NULL, `create_...

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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

WeChat applet implements image upload function WeChat applet implements image upload function Nov 21, 2023 am 09:08 AM

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

See all articles