Heim > Backend-Entwicklung > PHP-Tutorial > 仙剑奇侠传5前传支线 TP框架多上传域上传图片

仙剑奇侠传5前传支线 TP框架多上传域上传图片

WBOY
Freigeben: 2016-07-28 08:25:30
Original
1256 Leute haben es durchsucht

问题:

学习使用TP框架做电商网站是,添加商品表单需要上传商品logo和商品图片pics,有两个上传域,第一个上传域是logo,只上传一张,第二个上传域是pics,上传多张图片。使用如下代码,总是报错:非法上传文件!

<span>$upload</span> =<span> \Think\Upload;
</span><span>$info</span>=<span>$upload</span>->upload();
Nach dem Login kopieren

解决方法:

有多个上传域时,应如下使用upload方法,指定要处理的上传域:

$upload = \Think\Upload;
$info1 = $upload->upload(array('logo'=>$_FILES['logo']);
$info2 = $upload->upload(array('pics'=>$_FILES['pics']);
Nach dem Login kopieren

参考资料:

Title:

TP框架上传图片时报错“非法上传"处理

Content:

TP框架上传图片时报错“非法上传"处理,主要注意以下两点:

1.$_FILES中有多个上传域问题,所以针对不同的上传域,区别命名;

2.如同个上传域中有一些没有上传图片,则要重新收集好上传的信息(收集成非空的一维数组);

具体代码如下;

第一种情况:

<span>if</span>(<span>$this</span>-><span>_hasImg())  
</span>02.<span>                {  
</span>03.                    <span>//</span><span> 只上传表单中name叫做jqimg的图片  </span>04.                    <span>$info</span> = <span>$upload</span>->upload(<span>array</span>('jqimg'=><span>$_FILES</span>['jqimg'<span>]));  
</span>05.                    <span>if</span>(!<span>$info</span><span>)  
</span>06.                        redirect(U('Ad/Ad/save/id/'.<span>$ad_id</span>), 3, <span>$upload</span>-><span>getError());  
</span>07.                    <span>//</span><span> 循环每一张图片,插入到jq-info表  </span>08.                    <span>foreach</span> (<span>$info</span><span>as</span><span>$k</span> => <span>$v</span><span>)  
</span>09.<span>                    {  
</span>10.                        <span>$url</span> = <span>$v</span>['savepath'] . <span>$v</span>['savename'<span>];  
</span>11.                        <span>$this</span>->execute("INSERT INTO sh_ad_jq_info(img_url,ad_link,ad_id) VALUES('<span>$url</span>','{<span>$_POST</span>['jqtxt'][<span>$k</span>]}',<span>$ad_id</span>)"<span>);  
</span>12.<span>                    }  
</span>13.<span>                }  
</span>14.                <span>//</span><span> 处理修改的图片  </span>15.                <span>if</span>(<span>isset</span>(<span>$_POST</span>['oldjqtxt'<span>]))  
</span>16.<span>                {  
</span>17.                    <span>//</span><span> 先上传图片修改的图片  </span>18.                    <span>$info</span> = <span>$upload</span>->upload(<span>array</span>('oldjqimg'=><span>$_FILES</span>['oldjqimg'<span>]));  
</span>19.                    <span>$_k</span> = 0<span>;  
</span>20.                    <span>foreach</span> (<span>$_POST</span>['oldjqtxt'] <span>as</span><span>$k</span> => <span>$v</span><span>)  
</span>21.<span>                    {  
</span>22.                        <span>//</span><span> 有没有上传新图片  </span>23.                        <span>if</span>(<span>$info</span> && <span>isset</span>(<span>$info</span>[<span>$_k</span><span>]))  
</span>24.<span>                        {  
</span>25.                            <span>//</span><span> 上传了新图片  </span>26.                            <span>//</span><span> 先删除原图  </span>27.                            <span>$_ourl</span> = <span>$this</span>->query('SELECT img_url FROM sh_ad_jq_info WHERE id = '.<span>$k</span><span>);  
</span>28.                            @<span>unlink</span>(IMG_URL_HD . <span>$_ourl</span>[0]['img_url'<span>]);  
</span>29.                            <span>//</span><span> 再更新为新图  </span>30.                            <span>$_url</span> = <span>$info</span>[<span>$_k</span>]['savepath'] . <span>$info</span>[<span>$_k</span>]['savename'<span>];  
</span>31.                            <span>$this</span>->execute("UPDATE sh_ad_jq_info SET ad_link='<span>$v</span>',img_url='<span>$_url</span>' WHERE id=<span>$k</span>"<span>);  
</span>32.<span>                        }  
</span>33.                        <span>else</span>
34.<span>                        {  
</span>35.                            <span>$this</span>->execute("UPDATE sh_ad_jq_info SET ad_link='<span>$v</span>' WHERE id=<span>$k</span>"<span>);  
</span>36.<span>                        }  
</span>37.                        <span>$_k</span>++<span>;  
</span>38.<span>                    }  
</span>39.<span>                }  
</span>40.            }  
Nach dem Login kopieren

第二种情况:

<span>foreach</span>(<span>$_FILES</span>['imgjq_url']['tmp_name'] <span>as</span><span>$k</span>=><span>$v</span><span>)  
</span>02.<span>            {  
</span>03.  
04.                <span>if</span>(<span>$v</span><span>)  
</span>05.<span>                {  
</span>06.                    <span>//</span><span>删除原有硬盘图片  </span>07.                    <span>$img_url</span> = <span>$this</span>->query("SELECT imgjq_url FROM sh_ad_jq_info WHERE id = <span>$k</span>"<span>);  
</span>08.  
09.                    @<span>unlink</span>(IMG_URL_HD .<span>$img_url</span>[0]['imgjq_url'<span>] );  
</span>10.  
11.  
12.                    <span>//</span><span>收集图片信息  </span>13.                    <span>$arr</span> = <span>array</span><span>();  
</span>14.                    <span>$arr</span>['name'] = <span>$_FILES</span>['imgjq_url']['name'][<span>$k</span><span>];  
</span>15.                    <span>$arr</span>['type'] = <span>$_FILES</span>['imgjq_url']['type'][<span>$k</span><span>];  
</span>16.                    <span>$arr</span>['tmp_name'] = <span>$_FILES</span>['imgjq_url']['tmp_name'][<span>$k</span><span>];  
</span>17.                    <span>$arr</span>['error'] = <span>$_FILES</span>['imgjq_url']['error'][<span>$k</span><span>];  
</span>18.                    <span>$arr</span>['size'] = <span>$_FILES</span>['imgjq_url']['size'][<span>$k</span><span>];  
</span>19.  
20.  
21.                    <span>//</span><span>上传图片处理  </span>22.                    <span>$imgInfo</span> = <span>$upload</span>->upload(<span>array</span>('imgjq_url' =><span>$arr</span><span> ));  
</span>23.  
24.                    <span>if</span> (!<span>$imgInfo</span><span>) {  
</span>25.                        <span>echo</span><span>$upload</span>->getError();<span>die</span><span>;  
</span>26.<span>                    }  
</span>27.                      
28.                    <span>$imgjq_url</span> = <span>$imgInfo</span>['imgjq_url']['savepath'].<span>$imgInfo</span>['imgjq_url']['savename'<span>];  
</span>29.  
30.                    <span>//</span><span>更新到数据库  </span>31.                    <span>$this</span>->execute("UPDATE sh_ad_jq_info SET imgjq_url = '<span>$imgjq_url</span>' WHERE id = <span>$k</span>"<span>);  
</span>32.  
33.<span>                }  
</span>34.            } 
Nach dem Login kopieren

以上就介绍了仙剑奇侠传5前传支线 TP框架多上传域上传图片,包括了仙剑奇侠传5前传支线方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage