【Foreword】
This article introduces how to implement thinkphp - uploading new images and deleting old images
【main body】
The principle is very simple, similar to the upload operation principle. However, a delete action was added before uploading.
(1) Add an operation to the controller
//addData method public function addData($post,$file){ // Determine if there is a file to upload if($file['error'] == '0'){ //1. Configuration array, define configuration $cfg = array( //Configure the upload path 'rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH ); $upload = new \Think\Upload($cfg);//2. Instantiate the upload class $info = $upload->uploadOne($file);//3. Upload operation and accept the upload result if ($info) { //Original image path, complete the field after success $post['picture'] = UPLOAD_ROOT_PATH . $info['savepath'] . $info['savename']; $post['filename'] = $info['name'];//Original name of the file $post['hasfile'] = 1;//Is there a file // make thumbnail $image = new \Think\Image(); //1. Instantiate the class $imgPath = WORKING_PATH.$post['picture'];//2. Open the picture and pass the picture path. Use the root path uniformly $image->open($imgPath); $image->thumb(100,100);//3. Make thumbnails, scale proportionally //4. Save the image, pass in the path -- full path (absolute path directory + file name) $image->save(WORKING_PATH.UPLOAD_ROOT_PATH.$info['savepath'].'thumb_'.$info['savename']); $post['thumb'] = UPLOAD_ROOT_PATH.$info['savepath'].'thumb_'.$info['savename'];//5. Complete the thumb field } } // Completion field addtime $post['addtime'] = time(); //add operation return $this->add($post); }
(2) Update operation
//update data save
public function updateData($post,$file){
if(!$file['error']){
$cfg = array(
'rootPath' => WORKING_PATH.UPLOAD_ROOT_PATH
);
$fileInfo = M('article')->where('id='.$post['id'])->find();
$string1=$fileInfo['thumb'];
$thumb=substr_replace($string1,'',0,15);
unlink(WORKING_PATH.UPLOAD_ROOT_PATH.$thumb);//Delete the original thumb
$string2=$fileInfo['picture'];
$picture=substr_replace($string2,'',0,15);
unlink(WORKING_PATH.UPLOAD_ROOT_PATH.$picture);//Delete the original picture
// upload new attachment
$upload = new \Think\Upload($cfg);
$info = $upload->uploadOne($file);
if($info){
$post['picture'] = UPLOAD_ROOT_PATH . $info['savepath'] . $info['savename'];
$post['filename'] = $info['name'];//Original name of the file
$post['hasfile'] = 1;
// make thumbnail
$image = new \Think\Image();
$imgPath = WORKING_PATH.$post['picture'];
$image->open($imgPath);
$image->thumb(100,100);
$image->save(WORKING_PATH.UPLOAD_ROOT_PATH.$info['savepath'].'thumb_'.$info['savename']);
$post['thumb'] = UPLOAD_ROOT_PATH.$info['savepath'].'thumb_'.$info['savename'];//5. Complete the thumb field
}else{
//upload failed
}
}else{
// If there is no file, do not process
}
【Summarize】
①The place marked in red above is to delete the original image
②Knowledge point: Does php remove the first few characters without knowing how long the string is?
$string='string'; $subject=substr_replace(string,'',0,3);
Here I define the working path in the entry file, so I need to intercept the concatenated string
//define the working path define('WORKING_PATH', str_replace('\\','/',__DIR__)); //Define the upload root directory define('UPLOAD_ROOT_PATH', '/Public/Upload/');
.