about the corrupt zip file in php
I am trying to let the browser download a zip file created by php. I can
see the browser downloading the zip file. However, the zip file cannot be
opened. It seems to be corrupted. I have googled all the morning and tried
many methods but no luck. So please help me to point out what is wrong in
my code. Thanks.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
//error reporting flag
//error_reporting(E_ALL);
//ini_set( 'display_errors','1');
ob_start();
try {
$dbh = new PDO("mysql:host=localhost;dbname=abc-4-sms", 'root',
'123456',
array( PDO::ATTR_PERSISTENT => true));
}
catch(PDOException $e) {
echo $e->getMessage();
}
$startdate = $_POST["startdate"];
$enddate = $_POST["enddate"];
$npcrno = $_POST["nplatestcrno"];
$npgjno = $_POST["nplatestgjno"];
$twcrno = $_POST["twlatestcrno"];
$twgjno = $_POST["twlatestgjno"];
$kltcrno = $_POST["kltlatestcrno"];
$kltgjno = $_POST["kltlatestgjno"];
$wpcrno = $_POST["wplatestcrno"];
$wpgjno = $_POST["wplatestgjno"];
$khcrno = $_POST["khlatestcrno"];
$khgjno = $_POST["khlatestgjno"];
$cwbcrno = $_POST["cwblatestcrno"];
$cwbgjno = $_POST["cwblatestgjno"];
$ltcrno = $_POST["ltlatestcrno"];
$ltgjno = $_POST["ltlatestgjno"];
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '123456';
$conn=mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error
connecting to mysql');
$dbname = 'abc-4-sms';
mysql_select_db($dbname);
$stmt = $dbh->prepare("call
payment(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
if (!$stmt)
echo "Failed to prepare statement: (" .
print_r($pdo->errorInfo()) . ")\n";
try{
$stmt->bindParam(1, $startdate, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(2, $enddate, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(3, $npcrno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(4, $npgjno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(5, $twcrno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(6, $twgjno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(7, $kltcrno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(8, $kltgjno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(9, $wpcrno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(10, $wpgjno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(11, $khcrno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(12, $khgjno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(13, $cwbcrno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(14, $cwbgjno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(15, $ltcrno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
$stmt->bindParam(16, $ltgjno, PDO::PARAM_STR
|PDO::PARAM_INPUT_OUTPUT, 10);
}
catch (Exception $e)
{
echo $e->getMessage();
}
$success = $stmt->execute($params);
//echo "Issueing SQL prepared statement\n";
//$stmt->debugDumpParams() . "\n";
if (!$success) {
echo "Failed to execute statement: \n=======\n";
$arr = $stmt->errorInfo();
print_r($arr);
echo "\n========\n";
}
/*
if ($row = $stmt->fetch(PDO::FETCH_ASSOC))
echo "Query Result: " . print_r($row) . "\n";
else
echo "Query failed\n";
*/
// call the stored procedure
//$stored=$stmt->execute();
//print "procedure returned $stored\n";
$select = "SELECT * FROM npnormalcr ORDER BY `•Òåj #`, '¶Š¼'
desc";
$export = mysql_query ( $select ) or die ( "Sql error : " .
mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ( $i = 0; $i < $fields; $i++ )
{
$headercolumn .= mysql_field_name( $export , $i ) . "\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = '';
foreach( $row as $value )
{
if ( ( !isset( $value ) ) || ( $value == "" ) )
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
if ( $data == "" )
{
$data = "\n(0) Records Found!\n";
}
//remove all carriage returns from the data
$data = str_replace( "\r" , "" , $data );
$fpc1=file_put_contents("c:\\payment\\file1.txt", "abc");
$fpc2=file_put_contents("c:\\payment\\file2.txt",
"$headercolumn"."\n"."$data");
$your_data = "This is the data to be stored in the text file.";
// Open the file and erase the contents if any
$fp = fopen("c:\\textfile_name.txt", "w");
if (!fopen('/tmp/non-existant-file.txt', 'r')) {
var_dump($php_errormsg);
}
// My Apache server doesn't have the right to read this file
if (!fopen('/tmp/vboxdrv-Module.symvers', 'w')) {
var_dump($php_errormsg);
}
// Write the data to the file
$fw=fwrite($fp, $your_data);
// Close the file
fclose($fp);
Zip('c:\\payment', 'c:\\payment\\payment.zip');
set_include_path('c:\payment');
if (file_exists('c:\\payment\\payment.zip')) {
//Set Headers:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0,
pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s',
filemtime('c:\payment\payment.zip')) . ' GMT');
header('Content-Type: application/force-download');
header('Content-Disposition: inline;
filename="payment.zip"');
//header("Content-Type: application/octet-stream");
//header("Content-Disposition: attachment;
filename="your-file.jpg");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' .
filesize('c:\payment\payment.zip'));
header('Connection: close');
// add these two lines
ob_clean(); // discard any data in the output buffer (if
possible)
flush(); // flush headers (if possi
readfile("payment.zip");
exit();
}
if (file_exists('c:\\payment\\payment.zip')) {
unlink('c:\\paymenet\\payment.zip');
}
function Zip($source, $destination) {
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true) {
$files = new RecursiveIteratorIterator(new
RecursiveDirectoryIterator($source),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$file = str_replace('\\', '/', realpath($file));
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/',
'', $file . '/'));
} else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/',
'', $file), file_get_contents($file));
}
}
} else if (is_file($source) === true) {
$zip->addFromString(basename($source),
file_get_contents($source));
}
return $zip->close();
}
function deleteAll($directory, $empty = false) {
if (substr($directory, -1) == "/") {
$directory = substr($directory, 0, -1);
}
if (!file_exists($directory) || !is_dir($directory)) {
return false;
} elseif (!is_readable($directory)) {
return false;
} else {
$directoryHandle = opendir($directory);
while ($contents = readdir($directoryHandle)) {
if ($contents != '.' && $contents != '..') {
$path = $directory . "/" . $contents;
if (is_dir($path)) {
deleteAll($path);
} else {
unlink($path);
}
}
}
closedir($directoryHandle);
if ($empty == false) {
if (!rmdir($directory)) {
return false;
}
}
return true;
}
}
ob_end_flush();
?>
</body>
</html>
No comments:
Post a Comment