Home php教程 php手册 php parses .csv file

php parses .csv file

Aug 15, 2016 pm 04:49 PM

<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>
Copy after login

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)