php 如何输出csv文档
如何创建一个CSV文件
方法1 - 使用HTTP头
至于在Word和Excel,您需要添加头信息到PHP脚本的例子。
下面的代码片断创建一个指定的表包括其列名CSV文件。然后会提示用户下载此文件。
<?php
$table = 'table_name';
$outstr = NULL;
header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=cars-models.csv");
$conn = mysql教程_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("db",$conn);
// Query database to get column names
$result = mysql_query("show columns from $table",$conn);
// Write column names
while($row = mysql_fetch_array($result)){
$outstr.= $row['Field'].',';
}
$outstr = substr($outstr, 0, -1)." ";
// Query database to get data
$result = mysql_query("select * from $table",$conn);
// Write data rows
while ($row = mysql_fetch_assoc($result)) {
$outstr.= join(',', $row)." ";
}
echo $outstr;
mysql_close($conn);
?>
方法2 - 使用fputcsv()
在fputcsv()函数格式作为CSV行并将其写入一个打开的文件。欲了解更多信息,看
一看http://php.net/manual/en/function.fputcsv.php看看。
下面的代码片断创建一个指定表的列名,包括CSV文件,并将其发送到浏览器。
<?php
$table = 'table_name';
$filename = tempnam(sys_get_temp_dir(), "csv");
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("db",$conn);
$file = fopen($filename,"w");
// Write column names
$result = mysql_query("show columns from $table",$conn);
for ($i = 0; $i < mysql_num_rows($result); $i++) {
$colArray[$i] = mysql_fetch_assoc($result);
$fieldArray[$i] = $colArray[$i]['Field'];
}
fputcsv($file,$fieldArray);
// Write data rows
$result = mysql_query("select * from $table",$conn);
for ($i = 0; $i < mysql_num_rows($result); $i++) {
$dataArray[$i] = mysql_fetch_assoc($result);
}
foreach ($dataArray as $line) {
fputcsv($file,$line);
}
fclose($file);
header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=cars-models.csv");
// send file to browser
readfile($filename);
unlink($filename);
?>
$now_time = time(); //当前时间
$for_time = mktime(0,0,0,2,24,2010);//设置一个日期
$n_time = $now_time-$for_time;
if( $n_time > 2592000 )
{
$echo = round($n_time/2592000).'月前';
}
elseif( $n_time > 6048000 )
{
$echo = round($n_time/604800).'周前';
}
elseif( $n_time > 86400 )
{
$echo = round($n_time/86400).'天前';
}
elseif( $n_time > 3600 )
{
$echo = round($n_time/3600).'小时前';
}
elseif( $n_time > 60 )
{
$echo = round($n_time/60).'分钟前';
}
else
{
$echo = $n_time.'秒前';
}
echo $echo;
<html>
<head><title>Mail sender</title></head>
<body>
<form action=”mail.php” method=”POST”>
<b>Email</b><br>
<input type=”text” name=”email” size=40>
<p><b>Subject</b><br>
<input type=”text” name=”subject” size=40>
<p><b>Message</b><br>
<textarea cols=40 rows=10 name=”message”></textarea>
<p><input type=”submit” value=” Send “>
</form>
</body>
</html>
该窗体包含必要的文本字段电子邮件,主题,消息和发送按钮。该生产线<form action="mail.php" method="POST">告诉浏览器的PHP文件将处理的形式和使用什么方法发送data.When用户在填好表格,并点击发送按钮, mail.php文件被称为...
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];
/*Validation of subject and also mail */
if (!preg_match(”/w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/”, $email)) {
echo “<h4>Invalid email address</h4>”;
echo “<a href=’javascript教程:history.back(1);’>Back</a>”;
} elseif ($subject == “”) {
echo “<h4>No subject</h4>”;
echo “<a href=’javascript:history.back(1);’>Back</a>”;
}
/* Sends the mail and outputs the “Thank you” string if the mail is successfully sent, or the error string
otherwise. */
elseif (mail($email,$subject,$message)) {
echo “<h4>Thank you for sending email</h4>”;
} else {
echo “<h4>Can’t send email to $email</h4>”;
}
?>
</body>
</html>
<?php
$height = 300;
$width = 300;
$image = ImageCreate($width, $height);
$grey = ImageColorAllocate($image, 125, 125, 125);
$blue = ImageColorAllocate($image, 0, 0, 255);
$red = ImageColorAllocate($image, 255, 0, 0);
ImageString ($image, 4, 50, 50, "Size 4 Font", $red);
ImageString ($image, 5, 50, 100, "Size 5 Font", $blue);
ImageLine($image, 0, 0, 300, 300, $blue);
header ("Content-type: image/png");
ImagePng($image);
ImageDestroy($image);
?>
本教程将介绍如何创建使用PHP的飞行缩略图。此外您将学习如何处理的图像整个文件夹,并创建自己的缩略图。因为这需要GD库,您将需要至少有一个广东2.0.1 PHP安装启用。
下面我们将创建一个PHP脚本,它包含两种功能。第一个扫描所提供的任何目录。JPG图像,以及对每一个,创建一个指定的文件夹缩略图使用GD的图像功能。第二个函数中创建一个作为脚本,它包含了一些原始图像的链接的缩略图同一目录中的所有HTML文件。这可能是先进的图片库软件的基础。
下面的代码创建一个名为createThumbs,将获得3个参数的函数。第一和第二的是相应的目录,其中包含原始图像和进行该缩略图将被放置的目录路径。第三个参数是你的宽度为缩略图想要的。
<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
createThumbs("upload/","upload/thumbs/",100);
?>
先,我们通过它打开的图像和遍历目录,都在寻找。JPG文件。接下来,我们创建的目录中的每个图像的缩略图。要创建一个缩略图,我们在阅读使用imagecreatefromjpeg()函数文件,并计算新的缩略图大小。 imagesx()和imagesy()函数返回的宽度和高度分别形象。接下来,我们创建一个新的形象,用imagecreatetruecolor()。最后,我们复制并调整其大小与imagecopyresized()函数的原始文件和保存imagejpeg缩略图()。
代码的第二部分,创建一个名为createGallery它获取两个参数的函数(在目录中的图片和缩略图的存储位置),并创建一个HTML网页,其中包含了一些原始图像的链接的缩略图所有的相对路径。
<?php
function createGallery( $pathToImages, $pathToThumbs )
{
echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Thumbnails</title></head>";
$output .= "<body>";
$output .= "<table cellspacing="0" cellpadding="2" width="500">";
$output .= "<tr>";
// open the directory
$dir = opendir( $pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign="middle" align="center"><a href="{$pathToImages}{$fname}">";
$output .= "<img src="{$pathToThumbs}{$fname}" border="0" />";
$output .= "</a></td>";
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir( $dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
$fhandle = fopen( "gallery.html", "w" );
fwrite( $fhandle, $output );
fclose( $fhandle );
}
createGallery("upload/","upload/thumbs/");
?>
相关文章
- 这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
- PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
pandas pd.read_csv()函数中parse_dates()参数的用法说明
这篇文章主要介绍了pandas pd.read_csv()函数中parse_dates()参数的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-05- index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04- 这篇文章主要介绍了c# 如何对CSV文件操作,帮助大家更好的理解和学习C#,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
- 这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
- 在PS中像素大小、文档大小有什么区别呢,这个估计很多初学者不清楚,下面我来给大家讲解一下,希望对你有帮助。 1、像素大小 通常用于显示屏显示的图片大小的调整。菜...2016-09-14
- 这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31- 这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
- 今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
- DataTable用于在.net项目中,用于缓存数据,DataTable表示内存中数据的一个表,在.net项目中运用C#将DataTable转化为CSV文件,接下来通过本文给大家提供一个通用的方法,感兴趣的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
- 这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
- 经常看到有人踩在了PHP路径的坑上面了,感觉有必要来说说PHP中相对路径的一些坑,以及PHP中绝对路径的使用,下面一起来看看。 ...2016-08-24
- 这篇文章主要介绍了thinkPHP中多维数组的遍历方法,以简单实例形式分析了thinkPHP中foreach语句的使用技巧,需要的朋友可以参考下...2016-01-12
- 这篇文章主要介绍了PHP正则表达式过滤html标签属性的相关内容,实用性非常,感兴趣的朋友参考下吧...2016-05-06
- 这篇文章主要为大家详细介绍了php构造方法中析构方法在继承中的表现,感兴趣的小伙伴们可以参考一下...2016-04-15