Home >PHP Framework >ThinkPHP >Use ftp to upload pictures in thinkphp
The image upload function should be extremely common. Here we refer to the integration method in the ThinkPHP framework to sort out the FTP image upload function, so that it is convenient to upload relevant information during background operations. Images are directly uploaded to the online image server to avoid slow loading of images accessed by large traffic and reduce website access pressure.
1. Front-end design
This is mainly for testing the implementation of functions, using the simplest design, which is both convenient for reference and conducive to later function expansion. The main code of upload.html is attached below. Pay special attention to the code circled in the red box. The css style is relatively simple. If necessary, you can refer to the source code below.
2. Background controller design
The main code of config.class.php is as follows, in which the designed table "conf "You only need to use two fields here - 'tag', 'value', you can use a simple varchar type
public function upload(){ if (IS_POST){ foreach ($_FILES as $key => $value){ $img = handleImg($key); $furl = C('REMOTE_ROOT').$img; if ($img){ ftp_upload($furl,$img); $saveData['value'] = $img; M('conf') ->where("tag = '".$key."'") ->save($saveData); } } $this->success('FTP 测试完成',U('Config/upload'),2); }else{ $imgUrl = M('conf') ->where("tag = 'upImg'") ->getField('value'); $this->assign('imgUrl',$imgUrl); $this->display(); } }
3. Configuration data
In the public configuration file, configure the following constant data. The reference code is as follows. Pay attention to the correctness of configuring the FTP account and password. This is just an example for security reasons.
//ftp(外网服务器)上传文件相关参数 'FTP_SEVER' => 'http://img.52zhenmi.com', //此地址,作为图片读取的位置 请上线前仔细确认 'FTP_HOST' => 'img.52zhenmi.com', 'WEB_SEVER' => 'http://img.52zhenmi.com', 'WEB_M_SERVER' => 'http://www.52zhenmi.com/m', 'FTP_NAME' => 'fexxxi',//ftp帐户 'FTP_PWD' => '1qxxxxxxw',//ftp密码 'FTP_PORT' => '21',//ftp端口,默认为21 'FTP_PASV' => true,//是否开启被动模式,true开启,默认不开启 'FTP_SSL' => false,//ssl连接,默认不开启 'FTP_TIMEOUT' => 60,//超时时间,默认60,单位 s 'REMOTE_ROOT' => '/',//图片服务器根目录
4. Import files
Take my code as an example. Two files are quoted here, and FTP.class.php is placed in '/ Library/Think' directory;
Upload.class.php is placed in the '/Library/Org/Net' directory. You can adjust the directory according to your own usage habits, as long as there is no problem when instantiating the path. That’s it.
5. Add public functions
Pay attention to adding the public functions used in step 2 above.
/** * 图片上传的公共处理方法 * @param string $fileName 图片上传的name * @return string 图片的存储路径 */ function handleImg($fileName){ if($_FILES[$fileName]['tmp_name'] != ""){ $img = $_FILES[$fileName]; $imgUrl = __ROOT__."/public"; $upload = new \Org\Net\Upload($img, $imgUrl); return $upload->main(); } }
FTP file upload function
function ftp_upload($remotefile,$localfile){ $ftp = new \Think\Ftp(); $data['server'] = C('FTP_HOST'); $data['username'] = C('FTP_NAME');//ftp帐户 $data['password'] = C('FTP_PWD');//ftp密码 $data['port'] = C('FTP_PORT');//ftp端口,默认为21 $data['pasv'] = C('FTP_PASV');//是否开启被动模式,true开启,默认不开启 $data['ssl'] = C('FTP_SSL');//ssl连接,默认不开启 $data['timeout'] = C('FTP_TIMEOUT');//超时时间,默认60,单位 s $info = $ftp->start($data); if($info){ if($ftp->put($remotefile,$localfile)){} } $ftp->close(); }
6, operation screenshot
7 , Tip
For this reference code, the public method handleImg() involved will first transfer the image to be uploaded to the root directory of the currently operated website, and then upload the image through ftp_upload() The first step of transferring to the corresponding image FTP server is redundant in terms of implementation steps. The main reason is that the test server during the development process does not meet the FTP account requirements, and at the same time, it must facilitate timely updates of online content modifications.
Recommended tutorial: "TP5"
The above is the detailed content of Use ftp to upload pictures in thinkphp. For more information, please follow other related articles on the PHP Chinese website!