【问题标题】:Save CSV created by PHP and MySQL to server将 PHP 和 MySQL 创建的 CSV 保存到服务器
【发布时间】:2018-07-23 16:59:42
【问题描述】:

我非常接近做我需要做的事情。该文件确实保存到正确的文件夹中,并且包含正确的信息。但是,它不保存“,”(逗号分隔)。该文件仅包含名称和电子邮件,如下所示:name,name@email。但它像这样保存它:namename@email.com

无论如何,这是我目前所拥有的:

<?php
include "config/config.inc.php";

$from_date = $_GET["from_date"];
$to_date = $_GET["to_date"];
$City = $_GET["City"];
$cityExplode = explode(",",$City);
$cityImplode = "'".implode("','", $cityExplode)."'";

$query = "SELECT * from $dbTablePrefixMaster WHERE Date BETWEEN '".$from_date."' AND '".$to_date."' AND City IN ($cityImplode) ORDER BY Date ASC";

$result = $db->query($query) or die(mysqli_error($db));


if($result->num_rows >0)
{
$delimiter = ",";
$filename = "Upload.csv"; // Trying to save file in server


//create a file pointer
$f = fopen('php://output', 'w');



while($row=$result->fetch_assoc())
{               
    $lineData =         array(stripslashes($row['Name']),stripslashes($row['Email']));

    fputcsv($f, $lineData);

}

fseek($f, 0);


fpassthru($f);

$filename = "Upload.csv"; // File to Save
file_put_contents("downloads/" . $filename, $lineData);

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');

exit;
}
else
{
//$_SESSION['sess_msg_fail'] = "Sorry, no record found.";
//header("Location: index.php");
}

?>

顺便说一句,我确实在这个网站上找到了一些技巧,它们帮助我走到了这一步,但我找不到逗号分隔问题的帮助。

谢谢大家!

【问题讨论】:

  • 首先,去掉$delimiter,所以应该是fputcsv($f, $lineData);
  • 其次,将header 指令移到exit; 行上方。
  • 感谢 Hackerman 的快速回复!我做了这两个,但到目前为止它正在做同样的事情。我编辑了上面的代码以反映您的建议。
  • @Hackerman 你还能看到什么我可以尝试的吗?
  • 您能否添加var_dump($lineData); 并在您的问题中添加输出,我需要看看变量的实际外观。

标签: php mysql csv


【解决方案1】:

截至发帖时,这对我有用。希望它对未来的人有所帮助:

<?php
include "config.php";

$from_date = $_GET["from_date"];
$to_date = $_GET["to_date"];
$City = $_GET["City"];
$cityExplode = explode(",",$City);
$cityImplode = "'".implode("','", $cityExplode)."'";

$query = "SELECT * from $dbTablePrefixMaster WHERE Date BETWEEN '".$from_date."' AND '".$to_date."' AND City IN ($cityImplode) ORDER BY Date ASC";
$result = $db->query($query) or die(mysqli_error($db));


if($result->num_rows >0)
{
$delimiter = ",";

$emailArr = array();

while($row=$result->fetch_assoc())
{       
    if(!in_array($row['Email'],$emailArr))
    {
        $fileContent.= "".stripslashes($row['Rep']).",".stripslashes($row['Name']).",".stripslashes($row['Last_Name']).",".stripslashes($row['Email']).",".stripslashes($row    ['Add1']).",".stripslashes($row['Add2']).",".stripslashes($row['City']).",".stripslashes($row['State']).",".stripslashes($row['Zip']).",".stripslashes($row['PH_Home']).",".stripslashes    ($row['PH_Work']).",".stripslashes($row['PH_Mobile']).",".stripslashes($row['Date']).",".stripslashes($row['Business_Name'])."\n";
        $emailArr[] = $row['Email'];
    }

}

$file = "../folder/folder/folder/Upload.csv"; // File to Save

chmod($file, 0777);
file_put_contents($file, $fileContent);
echo 'echo stuff here';
}
else
{
echo 'no record found';
exit;
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    相关资源
    最近更新 更多