> php教程 > php手册 > 본문

PHP 구문 분석 .csv 파일

WBOY
풀어 주다: 2016-08-15 16:49:46
원래의
1041명이 탐색했습니다.
<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>
로그인 후 복사

 

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿