Home php教程 php手册 用PHP将数据导入到Foxmail

用PHP将数据导入到Foxmail

Jun 21, 2016 am 09:10 AM
csv foxmail lt mysql quot

foxmail|数据

最近小阳做了一个PHP同学录,同学们自己在那里添加或修改姓名、邮箱、OICQ等信息,并保存在mySQL数据库里。某日小阳突然想到,如果能用PHP生成一个文件,供同学们下载并把这些资料导入他们的Foxmail地址簿中,那该多好啊!

说干就干,很快小阳便将这功能推出来了。那是如何实现的呢?这里仅以导出姓名、邮箱和OICQ三项予以说明。

要资料可以导入Foxmail地址簿,当然要先了解一下导入Foxmail地址簿的文件内容和格式。打开Foxmail4.2一个帐户的地址簿,在其菜单栏“工具”-“导入”中可以看到,Foxmail支持两种外部文件的导入:“CSV文件”和“Wab文件”。我们选择生成CSV文件。那么可以导入Foxmail的CSV文件的内容和格式如何呢?让我们先从Foxmail导出一个CSV文件看一下。在Foxmail地址簿中选择一个记录不为空的文件夹,执行“工具”-“导出”- “文本文件”,保存文件名为“TEMP.CSV”, 在“下一步”的“请选择输出字段”中选择“姓名”、“电子邮箱地址”和“OICQ”,点击“完成”后便在指定路径生成了“TEMP.CSV”文件了。如果你装了微软office系列,会发现这是一个用EXCEL默认打开的文件,实际上它是EXCEL的逗号分隔值文件,双击打开后,其界面如图所示。



我们在这种情况下还是无法知道它的写入格式的。将“TEMP.CSV”文件改为用记事本打开,便可以发现它的格式非常简单:如图,文件第一行是Foxmail地址簿的字段,其它行是字段对应的值,各字段和值用英文逗号分隔。所以我们在PHP中按这种格式生成CSV文件,别人就可以下载并导入他们的Foxmail中了!



不过还有一个问题需要解决,那就是既然文件中以逗号作为分隔值,如果数据库记录中有英文逗号(注:以下符号若无特别说明皆指英文符号)怎么办?当然你可以先将数据中的逗号替换为中文逗号,但其实还有一个方法,那就是如果CSV文件相应的字段两端加上双引号(“即" "”)作分隔值,其间的英文逗号不作为分隔值,且字段中连续两个英文双引号(即“""”)也只作为一个显示,而不作为分隔值。

有了这些认识,我们就可以编写导出CSV文件的PHP文件了:


//这一行一定要放在程序的开始地方,不能有空格或换行。因为下面的header()函数不允许在使用前向用户输出任何东西。
$dfname="tofoxmail.csv"; //生成的文件名
//连接mySQL数据库:
  mysql_connect("localhost","yourname","yourpassword") or die("不能连接数据库!");
  mysql_select_db("alumni") or die("数据库发生错误!");
if($action=="downit"){
  $getdata=mysql_query("SELECT name,email,oicq FROM classdata"); //选择数据表中指定记录
//如果没有资料,则:
  if(@mysql_num_rows($getdata)==0){
  echo "对不起,还没有任何资料!";
  exit;
  }
  //以下生成一个文件供下载:
  header("Content-disposition: filename=$dfname");
  header("Content-type: unknown/unknown");
  echo "姓名,电子邮件地址,OICQ,foxaddrID"; //输出第一行,Foxmail地址簿的字段
  $i=1;
  while($row=mysql_fetch_array($getdata)){ //取得数据类型的值
  //将数据中每一个双引号替换为两个:
  $row[name]=str_replace("\"","\"\"",$row[name]);
  $row[email]=str_replace("\"","\"\"",$row[email]);
  $row[oicq]=str_replace("\"","\"\"",$row[oicq]);
  //输出相应字段对应的值的行,每个值用双引号和逗号作分隔符:
  echo base64_decode("DQo=")."\"$row[name]\",\"$row[email]\",\"$row[oicq]\",$i";
  /*上式中“base64_decode("DQo=")”是换行符,之所以不用“\n”,是因为两者不完全相同,
  用后者将可能导致Foxmail导入失败。
  */
  $i++;
  }
  exit;
  }
  ? >
  
  
  
  
  导出资料到Foxmail地址簿
   点击这里下载文件,保存并导入到您的Foxmail地址簿中。
  操作方法
  ……
  
  
在服务器上运行上述PHP文件,下载回来的“tofoxmail.csv”文件用记事本打开如图所示。

在Foxmail地址簿中点击“工具”-“导入”-“CSV文件”……,数据库中一大堆的资料一下子就全被导入了,这个主意还不错吧!



(以上程序在Apache+PHP4+mySQL和IIS+PHP4+mySQL中皆测试通过。)



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)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles