{"id":626,"date":"2023-02-26T11:07:40","date_gmt":"2023-02-26T03:07:40","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=626"},"modified":"2023-04-29T20:16:31","modified_gmt":"2023-04-29T12:16:31","slug":"upload-and-process-of-laravel-admin-files","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/upload-and-process-of-laravel-admin-files\/","title":{"rendered":"laravel-admin\u6587\u4ef6\u4e0a\u4f20\u53ca\u5904\u7406"},"content":{"rendered":"<ul>\n<li>file\u8868\u5355\u901a\u8fc7move\u6307\u5b9a\u4e0a\u4f20\u76ee\u5f55<\/li>\n<li>saving\u4fdd\u5b58\u56de\u8c03\u8bbe\u7f6e\u6587\u4ef6\u540d\u53ca\u6587\u4ef6\u6821\u9a8c\u53c2\u6570<\/li>\n<li>saved\u4fdd\u5b58\u56de\u8c03\u5b9e\u73b0\u540e\u671f\u5904\u7406\uff0c\u5982\u6587\u4ef6\u91cd\u547d\u540d\u7b49<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<pre><code class=\"language-php\">$form-&gt;hidden(&#039;file_name&#039;, &#039;\u66f4\u65b0\u6587\u4ef6\u540d&#039;);\n$form-&gt;file(&#039;file_path&#039;, &#039;\u66f4\u65b0\u6587\u4ef6&#039;)-&gt;move(function ($file_path) use ($form) {\n    if (!empty($form-&gt;app_id)) {\n        Log::info(&#039;Move App ID: &#039;. $form-&gt;app_id);\n        $app = AndroidApp::where(&#039;id&#039;, $form-&gt;app_id)-&gt;get([&#039;name&#039;])-&gt;first();\n        Session::put(&#039;app&#039;, $app);\n    } else {\n        $app = Session::get(&#039;app&#039;);\n    }\n    return &#039;files\/&#039; . $app-&gt;name;\n});\n\n$form-&gt;hidden(&#039;size&#039;, &#039;Size&#039;);\n$form-&gt;hidden(&#039;md5&#039;, &#039;Md5&#039;);\n$form-&gt;hidden(&#039;sha1&#039;, &#039;Sha1&#039;);<\/code><\/pre>\n<pre><code class=\"language-php\">$form-&gt;saving(function ($form) {\n    $file = $form-&gt;file_path;\n    if ($file != null &amp;&amp; $file-&gt;isValid()) {\n        Log::info(&#039;App Id: &#039; . $form-&gt;app_id);\n        Log::info(&#039;Temp File: &#039; . $file);\n        Log::info(&#039;Original Name: &#039; . $file-&gt;getClientOriginalName());\n        $file_name = $file-&gt;getClientOriginalName();\n\n        $form-&gt;file_name = $file_name;\n        $form-&gt;size = filesize($file);\n        $form-&gt;md5 = md5_file($file);\n        $form-&gt;sha1 = sha1_file($file);\n    }\n});<\/code><\/pre>\n<pre><code class=\"language-php\">$form-&gt;saved(function ($form) {\n    $app_version = $form-&gt;model();\n\n    \/\/$app = AndroidApp::where(&#039;id&#039;, $app_version-&gt;app_id)-&gt;get([&#039;name&#039;])-&gt;first();\n    $app = $app_version-&gt;app;\n    Session::put(&#039;app&#039;, $app);\n\n    if ($form-&gt;file_path != null &amp;&amp; $form-&gt;file_path-&gt;isValid()) {\n        $extension = substr($form-&gt;file_name, strrpos($form-&gt;file_name, &#039;.&#039;) + 1);\n        if ($extension === &#039;apk&#039; || $extension === &#039;patch&#039; || $extension === &#039;tpatch&#039;) {\n            $extension = &#039;zip&#039;;\n        }\n        $file_name = substr($form-&gt;file_name, 0, strrpos($form-&gt;file_name, &#039;.&#039;) + 1) . $extension;\n\n        $old_file = public_path(&#039;upload\/&#039;) . $app_version-&gt;file_path;\n        $new_file = public_path(&#039;upload\/files\/&#039;) . $app-&gt;name . &#039;\/&#039; . $file_name;\n        Log::info(&#039;Old File: &#039; . $old_file);\n        Log::info(&#039;New File: &#039; . $new_file);\n\n        \/\/\u4e0a\u4f20\u540c\u540d\u6587\u4ef6\u4f1a\u5bfc\u81f4md5\u81ea\u52a8\u547d\u540d\uff0c\u540e\u671f\u91cd\u547d\u540d\u56de\u6765\n        if (file_exists($old_file)) {\n            if (rename($old_file, $new_file)) {\n                Log::info(&#039;Rename Success&#039;);\n                $app_version[&#039;file_path&#039;] = &#039;files\/&#039; . $app-&gt;name . &#039;\/&#039; . $file_name;\n                $app_version-&gt;saveOrFail();\n            } else {\n                Log::info(&#039;Rename Failure&#039;);\n            }\n        }\n    }\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>file\u8868\u5355\u901a\u8fc7move\u6307\u5b9a\u4e0a\u4f20\u76ee\u5f55 saving\u4fdd\u5b58\u56de\u8c03\u8bbe\u7f6e\u6587\u4ef6\u540d\u53ca\u6587\u4ef6\u6821\u9a8c\u53c2\u6570 saved\u4fdd\u5b58\u56de\u8c03\u5b9e\u73b0\u540e\u671f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[75],"class_list":["post-626","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel-admin"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/626","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=626"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/626\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=626"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=626"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=626"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}