PHP implementation of markdown article upload to seven cattle map bed instance content


After using markdown to write articles, we often need to publish them to different platforms. There is a problem here. The pictures of articles need to be uploaded manually, which is very inconvenient to manage. Therefore, it is strongly recommended to upload the pictures to the drawing bed, so that an article can be easily synchronized to the major platforms. Next, use PHP to realize this function, and select seven cattle cloud as the drawing bed

Create and enter project

$ mkdir markdown-images-to-qiniu

$ cd markdown-images-to-qiniu

Installation of seven cattle official extension

$ composer require qiniu/php-sdk

The idea is very simple

Read makrdown file

Regular matching of all images

● upload pictures in turn

Replace the address of the article picture with the address of the drawing bed

Save the replaced article

The following is the specific implementation. First, create the script index. PHP in the project directory,


require 'vendor/autoload.php';

use Qiniu\Auth;

use Qiniu\Storage\UploadManager;

//1. Read the 'makrdown' file

$file = $argv[1];

if(! file_exists($file) ){

  Return "file {$file} not found.";


$orginalContent = file_get_contents($file);

//2. Match all the pictures regularly







$mdImageArr = $matches[0];

if(! count($mdImageArr) ){

  Return "no need to upload pictures.";


//3. Upload pictures in turn

$accessKey ='your accessKey ';

$secretkey ='your secretkey ';

$bucket ='your space name '// eg. mindgeek

$url = the domain name to which the space is bound// eg.

$auth = new Auth($accessKey, $secretKey);

$token = $auth->uploadToken($bucket);

$uploadMgr = new UploadManager();

$content = $orginalContent;

foreach ($mdImageArr as $image) {

  $start = mb_strpos($image, '](') + 2;

  $localPath = mb_substr($image, $start, -1);

  $extension = pathinfo($localPath)['extension'];

  $uploadPath = uniqid(). ".". $extension;

  list($ret, $error) = $uploadMgr->putFile($token, $uploadPath, $localPath);

  if(! $error ){

    //4. Replace the address of the article picture with the address of the drawing bed

    $content = str_replace($localPath, $url.$uploadPath, $content);

    Echo "{$uploadpath} successfully uploaded\ n";

  } else {

    Echo "{$uploadpath} upload failed\ n";



//5. Save the replaced article

file_put_contents($file, $content);


$ php index.php

The above is the PHP script to achieve markdown article uploaded to the seven cattle map bed details, if you have any additional content, you can contact the developer editor.

Recommended Today

Clarify the relationship between JS, ES and ts

preface I believe many front-end partners are rightECMAScript,JavaScript,TypeScriptThese words are quite familiar and can be said to be the core technology of our work. However, if you specifically ask what is the relationship and difference between the three, I’m afraid many small partners, like Xiaohei, are difficult to accurately describe them, so Xiaohei decided to […]