This article will list some commonly used PHP regular rules to extract images from articles, and replace and move image directory codes. I hope this article will be helpful to everyone.
When uploading pictures, if not all the uploaded pictures are used, you can set it to only save it in a temporary folder when uploading. When the article is published, move the actually used pictures to a useful directory, so that regular cleaning is useless. Directory will do.
代码如下 | 复制代码 |
//转移临时文件夹中的图片 $imgssss = preg_match_all("/linshi/[^s'"]+.jpg|png|gif|jpeg{1}/ui",$content,$imgss); $i=1; foreach($imgss as $aimg){ if(is_string($aimg)){ if (file_exists($aimg)){ $newdir = "upload/".date("ymdhis")."/"; if (!file_exists($newdir)){ mkdir($newdir,0755,true); } $newname = $newdir.date("ymdhis").$i.".".pathinfo($aimg,PATHINFO_EXTENSION); rename($aimg,$newname); $content = str_replace($aimg,$newname, $content); } $i++; }elseif(is_array($aimg)){ foreach($aimg as $imga){ if (file_exists($imga)){ $newdir = "upload/".date("ymdhis")."/"; if (!file_exists($newdir)){ mkdir($newdir,0755,true); } $newname = $newdir.date("ymdhis").$i.".".pathinfo($imga,PATHINFO_EXTENSION); rename($imga,$newname); $content = str_replace($imga,$newname, $content); } $i++; } } } |
If you just move a single image it is easier:
The code is as follows
|
Copy code | ||||
http: //www.bkjia.com/PHPjc/631500.html