Import and export csv files using php
Use php to import and export csv files
When I was working on some modules recently, I frequently import and exportcsv files. Compared with excel, its processing is simpler, the file size is smaller, and the data processing volume is large, and there are a lot of them. All languages are supported.
Not much nonsense, let’s get to the code~
Import code:
<code><span><<span>meta</span><span>charset</span> = "<span>utf-8</span>"></span><span><<span>form</span><span>method</span> = "<span>post</span>" <span>enctype</span> = "<span>multipart</span>/<span>form-data</span>" ></span><span><<span>h3</span>></span><strong>上传文件</strong><span></<span>h3</span>></span><span><<span>input</span><span>type</span> = "<span>file</span>" <span>name</span> = "<span>file</span>"></span><span><<span>button</span><span>type</span> = "<span>submit</span>" ></span>提交<span></<span>button</span>></span><span></<span>form</span>></span><span><span><?php</span><span>$csv</span> = <span>$_FILES</span>[<span>'file'</span>]; <span>$suffix</span> = array_pop(explode(<span>'.'</span>, <span>$csv</span>[<span>'name'</span>])); <span>//检查是否是csv文件</span><span>if</span>(<span>$suffix</span> <> <span>'csv'</span>) <span>exit</span>(<span>'请上传csv文件'</span>); <span>$file</span> = fopen(<span>$csv</span>[<span>'tmp_name'</span>], <span>'r'</span>); <span>$lines</span> = <span>array</span>(); <span>while</span>(<span>$data</span> = fgetcsv(<span>$file</span>)) <span>$lines</span>[] = <span>$data</span>; <span>echo</span><span>'<pre class="brush:php;toolbar:false">'</span>; print_r(<span>$lines</span>); <span>?></span></span></span></code>
Export code:
<code><span><<span>meta</span><span>charset</span> = "<span>utf-8</span>" /></span><span><<span>a</span><span>href</span> = "?<span>test</span>"></span>点我下载CSV文件<span></<span>a</span>></span><span><span><?php</span><span>if</span>(<span>isset</span>(<span>$_REQUEST</span>[<span>'test'</span>])){ ob_end_clean(); ob_start(); <span>//第一行题头</span><span>echo</span><span>"题头1,题头2,题头3,题头4\n"</span>; <span>//第一行数值</span><span>echo</span><span>"数值1,数值2,数值3,数值4\n"</span>; <span>$content</span> = ob_get_contents(); ob_end_clean(); header(<span>'Content-Type: text/html;charset=gb2312'</span>); header(<span>'Content-Type: application/force-download'</span>); header(<span>'Content-Disposition: attachment; filename=test.csv'</span>); <span>echo</span> iconv(<span>'utf-8'</span>, <span>'gb2312'</span>, <span>$content</span>); } <span>?></span></span></code>
Test link
Upload function test link
Download function test link
Above~ If you don’t understand, you can leave a message
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });The above introduces the use of PHP to import and export csv files, including uploading files, importing and exporting. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Laravel is one of the more outstanding PHP frameworks in the industry. Its powerful functions and easy-to-use API make it very popular among developers. In actual development, we often need to import and export data, and CSV, as a widely used data format, has also become one of the commonly used import and export formats. This article will introduce how to use LaravelExcel extension to import and export CSV files. 1. Install LaravelExcel First, we need

How to convert json string to csv format in php: 1. Create a php sample file; 2. Convert the JSON string to a PHP array or object; 3. Create a file handle and open a CSV file for writing; 4. Write the header row and data row in the CSV file; 5. Write the data row into the CSV file and use comma separators between fields, close the file handle and complete the conversion.

This article will explain in detail how PHP formats rows into CSV and writes file pointers. I think it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Format rows to CSV and write to file pointer Step 1: Open file pointer $file=fopen("path/to/file.csv","w"); Step 2: Convert rows to CSV string using fputcsv( ) function converts rows to CSV strings. The function accepts the following parameters: $file: file pointer $fields: CSV fields as an array $delimiter: field delimiter (optional) $enclosure: field quotes (

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Solution to the problem of garbled csv imported into PHP: 1. Construct a parsing function "function tb_str_getcsv($string, $delimiter=',', $enclosure='"') {...}"; 2. Read the file into a variable ;3. Remove the BOM header through "substr($s,2)".

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Java is a widely used programming language, and developers often need to deal with various data formats. CSV (Comma-SeparatedValues, comma-separated values) is a common data format widely used in data exchange and storage. In Java, we can use the OpenCSV library to read and write CSV files. OpenCSV is an easy-to-use open source library that provides a convenient API to process CSV data. This article explains how to
