PHP批量保存图片到服务器再上传阿里云

前端之家收集整理的这篇文章主要介绍了PHP批量保存图片到服务器再上传阿里云前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="cnblogs_code">


         </span><span style="color: #800080;"&gt;$num</span> = 50<span style="color: #000000;"&gt;;
    </span><span style="color: #800080;"&gt;$p</span> = <span style="color: #0000ff;"&gt;isset</span>(<span style="color: #800080;"&gt;$this</span>->request->get['p'])?<span style="color: #800080;"&gt;$this</span>->request->get['p']:0<span style="color: #000000;"&gt;;
    </span><span style="color: #800080;"&gt;$nonum</span> = <span style="color: #0000ff;"&gt;isset</span>(<span style="color: #800080;"&gt;$this</span>->request->get['nonum'])?<span style="color: #800080;"&gt;$this</span>->request->get['nonum']:0<span style="color: #000000;"&gt;;
    </span><span style="color: #800080;"&gt;$start</span> = (<span style="color: #800080;"&gt;$nonum</span>>0)?<span style="color: #800080;"&gt;$nonum</span>:<span style="color: #800080;"&gt;$p</span>*<span style="color: #800080;"&gt;$num</span><span style="color: #000000;"&gt;;

    </span><span style="color: #800080;"&gt;$query</span> = <span style="color: #800080;"&gt;$this</span>->db->query("SELECT product_id,image,site_id FROM " . DB_PREFIX . "product WHERE is_delete = 0 AND site_id='2170254929593344' AND image LIKE '%.alicdn.com%' LIMIT ".<span style="color: #800080;"&gt;$start</span>.",".<span style="color: #800080;"&gt;$num</span><span style="color: #000000;"&gt;);
    </span><span style="color: #800080;"&gt;$result</span> = <span style="color: #800080;"&gt;$query</span>-><span style="color: #000000;"&gt;rows;

    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>处理</span>
    <span style="color: #0000ff;"&gt;if</span>(!<span style="color: #0000ff;"&gt;empty</span>(<span style="color: #800080;"&gt;$result</span><span style="color: #000000;"&gt;)){

        </span><span style="color: #800080;"&gt;$aliYunOSS</span> = <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; AliYunOSS();

        </span><span style="color: #0000ff;"&gt;foreach</span> (<span style="color: #800080;"&gt;$result</span> <span style="color: #0000ff;"&gt;as</span> <span style="color: #800080;"&gt;$key</span> => <span style="color: #800080;"&gt;$value</span><span style="color: #000000;"&gt;){

            </span><span style="color: #800080;"&gt;$file</span> = <span style="color: #800080;"&gt;$value</span>['image'<span style="color: #000000;"&gt;];

            </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;保存<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>到服务器上</span>
            <span style="color: #800080;"&gt;$data</span> = @<span style="color: #008080;"&gt;file_get_contents</span>(<span style="color: #800080;"&gt;$file</span><span style="color: #000000;"&gt;);
            </span><span style="color: #0000ff;"&gt;if</span>(!<span style="color: #0000ff;"&gt;empty</span>(<span style="color: #800080;"&gt;$data</span><span style="color: #000000;"&gt;)){
                </span><span style="color: #800080;"&gt;$ext</span> = <span style="color: #008080;"&gt;pathinfo</span>(<span style="color: #800080;"&gt;$file</span>,<span style="color: #000000;"&gt; PATHINFO_EXTENSION);
                </span><span style="color: #800080;"&gt;$filename</span> = <span style="color: #800080;"&gt;$value</span>['product_id'].<span style="color: #008080;"&gt;rand</span>(). '.' . <span style="color: #800080;"&gt;$ext</span><span style="color: #000000;"&gt;;
                </span><span style="color: #800080;"&gt;$rootFile</span> = DIR_UPLOAD .'file/'. <span style="color: #800080;"&gt;$filename</span><span style="color: #000000;"&gt;;
                </span><span style="color: #008080;"&gt;file_put_contents</span>(<span style="color: #800080;"&gt;$rootFile</span>,<span style="color: #800080;"&gt;$data</span><span style="color: #000000;"&gt;);

                </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/shangchuan/" target="_blank" class="keywords">上传</a><a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>到阿里云</span>
                <span style="color: #800080;"&gt;$fileDir</span> = <span style="color: #008080;"&gt;date</span>('Y',<span style="color: #008080;"&gt;time</span>()) . '/' . <span style="color: #008080;"&gt;date</span>('m',<span style="color: #008080;"&gt;time</span><span style="color: #000000;"&gt;());
                </span><span style="color: #800080;"&gt;$ali</span> = <span style="color: #800080;"&gt;$aliYunOSS</span>->uploadFile(<span style="color: #800080;"&gt;$rootFile</span>,<span style="color: #800080;"&gt;$value</span>['site_id'].'/'.<span style="color: #800080;"&gt;$fileDir</span>.'/'.<span style="color: #800080;"&gt;$filename</span><span style="color: #000000;"&gt;);

                </span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #800080;"&gt;$ali</span>['success']==1 &amp;&amp; !<span style="color: #0000ff;"&gt;empty</span>(<span style="color: #800080;"&gt;$ali</span>['info']['url'<span style="color: #000000;"&gt;])){
                    </span><span style="color: #800080;"&gt;$image</span> = <span style="color: #800080;"&gt;$ali</span>['info']['url'<span style="color: #000000;"&gt;];

                    </span><span style="color: #0000ff;"&gt;if</span> (ENVIRONMENT == 'pro'<span style="color: #000000;"&gt;) {
                        </span><span style="color: #800080;"&gt;$image</span> = <span style="color: #008080;"&gt;str_replace</span>('http://joinf-website-release.oss-cn-hongkong.aliyuncs.com',OSS_URL,<span style="color: #800080;"&gt;$image</span><span style="color: #000000;"&gt;);
                        </span><span style="color: #800080;"&gt;$image</span> = <span style="color: #008080;"&gt;str_replace</span>('https://joinf-website-release.oss-cn-hongkong.aliyuncs.com',<span style="color: #800080;"&gt;$image</span><span style="color: #000000;"&gt;);
                    }

                    </span><span style="color: #800080;"&gt;$<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a></span> = "UPDATE " . DB_PREFIX . "product SET image='".<span style="color: #800080;"&gt;$image</span>."' WHERE product_id='".<span style="color: #800080;"&gt;$value</span>['product_id']."' AND image='".<span style="color: #800080;"&gt;$value</span>['image']."'"<span style="color: #000000;"&gt;;
                    </span><span style="color: #800080;"&gt;$this</span>->db->query(<span style="color: #800080;"&gt;$<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a></span><span style="color: #000000;"&gt;);

                    </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>服务器<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a></span>
                    <span style="color: #008080;"&gt;unlink</span>(<span style="color: #800080;"&gt;$rootFile</span><span style="color: #000000;"&gt;);
                }
            }</span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;{
                </span><span style="color: #800080;"&gt;$nonum</span>++<span style="color: #000000;"&gt;;
            }
        }

        </span><span style="color: #800080;"&gt;$url</span> = <span style="color: #800080;"&gt;$this</span>->url->createUrl('test/transferImage','p='.(<span style="color: #800080;"&gt;$p</span>+1).'&amp;nonum='.<span style="color: #800080;"&gt;$nonum</span>.'&amp;rand='.<span style="color: #008080;"&gt;rand</span>(1000,9999<span style="color: #000000;"&gt;));
        </span><span style="color: #0000ff;"&gt;echo</span> "<script>window.location.href='".<span style="color: #800080;"&gt;$url</span>."'</script>"<span style="color: #000000;"&gt;;

    }</span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;{
        </span><span style="color: #0000ff;"&gt;echo</span> '处理完成'<span style="color: #000000;"&gt;;
    }

}</span></pre>

 

原文链接:https://www.f2er.com/php/403003.html

猜你在找的PHP相关文章