网赌平台哪个信誉好:php读取excel文件(.csv)实例介绍

‘转换

<?php 
function getCSVdata($filename) 

    $row = 1;//第一行开始 
    if(($handle = fopen($filename, “r”)) !== false)  
    { 
        while(($dataSrc = fgetcsv($handle)) !== false)  
        { 
            $num = count($dataSrc); 
            for ($c=0; $c < $num; $c++)//列 column  
            { 
                if($row === 1)//第一行作为字段  
                { 
                    $dataName[] = $dataSrc[$c];//字段名称 
                } 
                else 
                { 
                    foreach
($dataName as $k=>$v) 
                    { 
                        if($k == $c)//对应的字段 
                        { 
                            $data[$v] = $dataSrc[$c]; 
                        } 
                    } 
                } 
            } 
            if(!empty($data)) 
            { 
                 $dataRtn[] = $data; 
                 unset($data); 
            } 
            $row++; 
        } 
        fclose($handle); 
        return $dataRtn; 
    } 

 
$aData = getCSVdata(‘all_www.111cn.net .csv’); 
 
foreach ($aData as $k=>$v ){
echo “http://".$v\['a'\]."&lt;br&gt;”;
}
?>  

  关闭设置的目录,然后处理:

例3.php数据导入导出之excel

今天教给大家如何用PHP实现把图象上传到MYSQL数据库中。
在这个教程中我们需要建立3个PHP文件:

 代码如下

具体的内容:
<?
$dbcnx = mysql_connect(“localhost”, “username”, “password”);
mysql_select_db(“base64imgdb”);
?>
‘我们需要打开一个目录
“./”
‘readdir.php 文件定位于这个目录:
$path = “./”;
$dir_handle = opendir($path) or die(“Unable to open directory
$path”);

phpExcelReader是专门用来读取文件的。返回一个数组,包含表格的所有内容。
该 class 使用的方法可以参考网站下载回来的压缩档中的 example.php。

‘ 插入到表里

<?php
$DB_Server = www.111cn.net;//这里是你的数据连接
$DB_Username = “a0530093319”;
$DB_Password = “123456”;
$DB_DBName = “a0530093319”;
$DB_TBLName = “member”;
$savename = date(“YmjHis”);
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die(“Couldn’t connect.”);
mysql_query(“Set Names ‘gb2312′”);
$file_type = “vnd.ms-excel”;
$file_ending = “xls”;
header(“Content-Type: application/$file_type”);
header(“Content-Disposition: attachment;
filename=”.$savename.”.$file_ending”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
$now_date = date(“Y-m-j H:i:s”);
$title =
“数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date”;
$sql = “Select * from $DB_TBLName”;
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die(“Couldn’t select database”);
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo(“$title “);
$sep = ” “;
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . ” “;
}
print(” “);
$i = 0;
while($row = mysql_fetch_row($result)) {
$schema_insert = “”;
for($j=0; $j<mysql_num_fields($result);$j++) {
if(!isset($row[$j]))
$schema_insert .= “NULL”.$sep;
elseif ($row[$j] != “”)
$schema_insert .= “$row[$j]”.$sep;
else
$schema_insert .= “”.$sep;
}
$schema_insert = str_replace($sep.”$”, “”, $schema_insert);
$schema_insert .= ” “;
print(trim($schema_insert));
print ” “;
$i++;
}
return (true);
?>

CREATE TABLE `images` (
`imgid` INT NOT NULL AUTO_INCREMENT ,
`sixfourdata` LONGTEXT NOT NULL ,
PRIMARY KEY ( `imgid` )
);
READDIR.PHP

PHP代码

1.创建一个数据库

</form>
<?
error_reporting(0);
//导入CSV格式的文件
$connect=mysql_connect(“localhost”,”a0530093319″,”123456″) or
die(“could not connect to database”);
mysql_select_db(“a0530093319”,$connect)
or die (mysql_error());
$fname = $_FILES[‘MyFile’][‘name’];
$handle=fopen(“$fname”,”r”);
while($data=fgetcsv($handle,10000,”,”))
{
$q=”insert into test (code,name,date) values
(‘$data[0]’,’$data[1]’,’$data[2]’)”;
mysql_query($q) or die (mysql_error());
}
fclose($handle);
?>

fopen

php读取excel文件(.csv)参考代码:

<?
while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == ‘gif’ OR $filetyp == ‘jpg’) {
$handle = fopen($path . “/” . $file,’r’);
$file_content = fread($handle,filesize($path . “/” . $file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content));
$sql = “INSERT INTO images SET sixfourdata=’$encoded'”;
mysql_query($sql);
}
}
?>

<?php
$row = 1;
$handle = fopen(“test.csv”,”r”);
while ($data = fgetcsv($handle, 1000, “,”)) {
 $num = count($data);
 echo “
 $num fields in line $row:n”;
 $row++;
 for ($c=0; $c < $num; $c++) {
 echo $data[$c] . “n”;
 }
}
fclose($handle);
?>

<?
closedir($dir_handle);
echo(“complete”);
mysql_close($dbcnx);
?>

 代码如下

  readdir.php – 把图片放到数据库的代码
  image.php – 显示实际图片的代码
  view.php – 显示你如何调用数据库中的图片的代码

例2

  下面是比较难的部分,大家需要好好研究一下:把图象分类,并且读出正在使用的一些数据

PHP有自带的分析.csv函数:fgetcsv

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图