Heim > php教程 > php手册 > PHP analysiert die CSV-Datei

PHP analysiert die CSV-Datei

WBOY
Freigeben: 2016-08-15 16:49:46
Original
1080 Leute haben es durchsucht
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> actionImport() {<br>     <span style="color: #00ff00;">//post请求过来的
        </span></span><span style="color: #800080;">$fileName</span> = <span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">];
        </span><span style="color: #800080;">$fileTmpName</span> = <span style="color: #800080;">$_FILES</span>['file']['tmp_name'<span style="color: #000000;">];
        </span><span style="color: #008000;">//</span><span style="color: #008000;">判断是否选择了上传的文件</span>
        <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$fileName</span><span style="color: #000000;">)) {
            </span><span style="color: #800080;">$data</span>['message'] = "请选择要上传的文件"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->render("batch_add", <span style="color: #800080;">$data</span><span style="color: #000000;">);
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">判断选择上传的文件是不是csv格式</span>
        <span style="color: #0000ff;">if</span> (<span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$fileName</span>)[1] != "csv"<span style="color: #000000;">) {
            </span><span style="color: #800080;">$data</span>['message'] = "请选择csv格式的文件上传"<span style="color: #000000;">;
            </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->render("batch_add", <span style="color: #800080;">$data</span><span style="color: #000000;">);
        } 

        </span><span style="color: #008000;">//</span><span style="color: #008000;">创建一个空数组,预放imageUrl</span>
        <span style="color: #800080;">$imageCollection</span> =<span style="color: #000000;"> [];
        </span><span style="color: #008000;">//</span><span style="color: #008000;">打开要读的文件</span>
        <span style="color: #800080;">$handle</span> = <span style="color: #008080;">fopen</span>(<span style="color: #800080;">$fileTmpName</span>, 'r'<span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;">解析csv文件</span>
        <span style="color: #0000ff;">while</span> (!<span style="color: #008080;">feof</span>(<span style="color: #800080;">$handle</span><span style="color: #000000;">)) {
            </span><span style="color: #008000;">//</span><span style="color: #008000;">fgets方法按行读</span>
            <span style="color: #800080;">$result</span> = <span style="color: #008080;">fgets</span>(<span style="color: #800080;">$handle</span><span style="color: #000000;">);
            </span><span style="color: #008000;">//</span><span style="color: #008000;">判断读到的每一行是否有值</span>
            <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">)) {
                </span><span style="color: #800080;">$arrResult</span> = <span style="color: #008080;">explode</span>(",", <span style="color: #800080;">$result</span><span style="color: #000000;">);
                </span><span style="color: #800080;">$name</span> = <span style="color: #800080;">$arrResult</span>[0<span style="color: #000000;">];
                </span><span style="color: #800080;">$age</span> = <span style="color: #800080;">$arrResult</span>[1<span style="color: #000000;">];
                </span><span style="color: #800080;">$gender</span> = <span style="color: #800080;">$arrResult</span>[2<span style="color: #000000;">];
                </span><span style="color: #008000;">//</span><span style="color: #008000;">图片的原路径</span>
                <span style="color: #800080;">$imagePath</span> = <span style="color: #800080;">$arrResult</span>[3<span style="color: #000000;">];
                </span><span style="color: #008000;">//</span><span style="color: #008000;">图片的名字</span>
                <span style="color: #800080;">$image</span> = <span style="color: #008080;">basename</span>(<span style="color: #800080;">$imagePath</span><span style="color: #000000;">);

                </span><span style="color: #800080;">$data</span>['message'] = Person::savePerson(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$age</span>, <span style="color: #800080;">$gender</span>, <span style="color: #800080;">$image</span><span style="color: #000000;">);

                </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$data</span>['message'] == 'add successful'<span style="color: #000000;">) {
                    </span><span style="color: #008000;">//</span><span style="color: #008000;">将每个图片的uri放到数组中</span>
                    <span style="color: #008080;">array_push</span>(<span style="color: #800080;">$imageCollection</span>, <span style="color: #800080;">$imagePath</span><span style="color: #000000;">);
                }
            }
        }
        </span><span style="color: #008000;">//</span><span style="color: #008000;">关闭文件流</span>
        <span style="color: #008080;">fclose</span>(<span style="color: #800080;">$handle</span><span style="color: #000000;">);

        </span><span style="color: #008000;">//</span><span style="color: #008000;">关闭文件流之后才能上传图片,注意:流和流是不能嵌套使用的</span>
        <span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$imageCollection</span><span style="color: #000000;">)) {

            </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$imageCollection</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$value</span><span style="color: #000000;">) {
                </span><span style="color: #008000;">//</span><span style="color: #008000;">将图片上传到服务器上</span>
                <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$imagePath</span>, <span style="color: #008080;">dirname</span>(__DIR__) . '/web/images/'.<span style="color: #008080;">date</span>("Ymd").'/'.<span style="color: #800080;">$image</span><span style="color: #000000;">);
            }
        }
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->render("batch_add", <span style="color: #800080;">$data</span><span style="color: #000000;">);
    }</span>
Nach dem Login kopieren

 

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 Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage