没有不值得去解决的问题,也没有不值得去学习的技术!

Write Strings to Files in Yii 2.0 Using Aliases With file_put_contents

The final written filename: /mcloud/www/channel-pub-api/console/runtime/qq-tp-transaction-video-sync-1596087411.7955-2004360950.txt. Its contents are as follows.

1. When writing a string to a file using the function file_put_contents, our team used various formats to define the target filename.

PHP

file_put_contents(Yii::$app->runtimePath . '/cmsTop.log', date('Y-m-d H:i:s') . ' '. $uri . '  ' . var_export($data, true), FILE_APPEND);

$bathPath = Yii::$app->getBasePath();
@file_put_contents($bathPath . '/runtime/responseResult.txt', var_export($url, true)."\n", LOCK_EX);

file_put_contents('/tmp/123', date('Y-m-d H:i:s') . ' bk_create_params' . var_export($data, 1) . PHP_EOL, FILE_APPEND);

file_put_contents('E:/wwwroot/channel-pub-api/console/runtime/upload-asset-image-exec-handler-' . $assetId . '-' . time() . '.txt', print_r($assetId, true));

2. The problem with these different formats is that as the program’s runtime environment changes, the filename for the written data also constantly changes. This leads to write failures, which could be due to path issues or permission issues. We ultimately decided to standardize on a single format: using Aliases to improve universality and the success rate.

PHP

file_put_contents(Yii::getAlias('@runtime') . '/qq-tp-transaction-video-sync-' . microtime(true) . '-' . mt_rand()  . '.txt', print_r($qqTransactionServiceHttpTransactionInfoResult, true), FILE_APPEND | LOCK_EX);

3. The final written filename: /mcloud/www/channel-pub-api/console/runtime/qq-tp-transaction-video-sync-1596087411.7955-2004360950.txt. Its contents are as follows. See Figure 1.

The final written filename: /mcloud/www/channel-pub-api/console/runtime/qq-tp-transaction-video-sync-1596087411.7955-2004360950.txt. Its contents are as follows.
Figure 1
Plaintext
Array
(
    [transaction_id] => 781154629713605046
    [transaction_status] => Success
    [ext_err] => 0
    [transaction_err_msg] =>
    [transaction_type] => Article
    [transaction_ctime] => 2020-07-24 16:32:22
    [article_info] => Array
        (
            [article_title] => 追梦身影·西迁往事之峥嵘岁月
            [article_type] => Video Article
            [article_abstract] => 追梦身影·西迁往事之峥嵘岁月
            [article_imgurl] => http://inews.gtimg.com/newsapp_ls/0/12144797745_196130/0
            [article_pub_flag] => Published Successfully
            [article_pub_time] => 2020-07-24 16:32:22
            [article_id] => 20200724V0MC8V
            [article_url] => http://kuaibao.qq.com/s/20200724V0MC8V00
            [article_video_info] => Array
                (
                    [vid] => e3121ha42ez
                    [title] => 追梦身影·西迁往事之峥嵘岁月
                    [duration] => 00:05:14
                    [desc] => 追梦身影·西迁往事之峥嵘岁月
                    [type] => video
                )

            [article_pid] =>
        )

)

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系