Wednesday, September 24, 2008

php function zip and stream 2

last time we use ziparchive but now
we'll use zip library from phpmyadmin
and never store data on server

function zipAndStream($filestostream,$dir,$flag=0){
  require_once("zip.lib.php"); // from phpmyadmin
  $zip = new zipfile();
    if(is_array ($filestostream)){
      foreach($filestostream as $filetostream){
        $filename = $dir."/".$filetostream;
        $fsize = @filesize($filename);
        $fh = fopen($filename, 'rb', false);
        $filedata = fread($fh, $fsize);
        $zip->addFile($filedata,$filename);
        $filecount++;
        $filenamefull = $filetostream;
        $filename = substr($filetostream,0,strrpos($filetostream,'.'));
      }
      if($filecount!=1){
        $filename = $filecount.'files';
      }
    }else{
      $filename = $dir."/".$filestostream;
      $fsize = @filesize($filename);
      $fh = fopen($filename, 'rb', false);
      $filedata = fread($fh, $fsize);
      $zip->addFile($filedata,$filename);
      $filecount++;
      $filename = substr($filestostream,0,strrpos($filestostream,'.'));
    }
  $zipcontents = $zip->file();
  if($flag==1){
    $imagefile = "php.png";
    $filesize = strlen($zipcontents)+filesize($imagefile);
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$filecount."files_".$imagefile."\"");
    header("Content-length: " . $filesize . "\n\n");
    readfile($imagefile);
  }elseif($flag==-1){
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$filenamefull."\"");
    header("Content-length: " . strlen($filedata) . "\n\n");
    echo $filedata;
    exit;
  }else{
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$filename.".zip\"");
    header("Content-length: " . strlen($zipcontents) . "\n\n");
  }
  echo $zipcontents;
}

Monday, September 22, 2008

javascript validate array checkbox

we can pass value through php and validate checkbox by these

<html>
<html>
<head>
<script language="JavaScript">
function check()
{
 var a=document.some_form['graduate[]'];
 alert("Length:"+a.length);
 var p=0;
 for(i=0;i<a.length;i++){
  if(a[i].checked){
   alert(a[i].value);
   p=1;
  }
 }
 if (p==0){
  alert('please select at least one check box');
  return false;
 }
 document.some_form.submitted.value='yes';
 return true;
}
</script>
</head>
<body>
<form name="some_form" onsubmit="return check();" method="POST" action="">
<table>
<tr><td>Post Graduate in</td><td>
<input type="checkbox" name="graduate[]" value="history">History
<input type="checkbox" name="graduate[]" value="telugu">Telugu
<input type="checkbox" name="graduate[]" value="Computer sceince">Computer Science
<input type="checkbox" name="graduate[]" value="Mathematics">Mathematics

</td></tr>
<tr><td><input type="submit" value="Submit"></td></tr>
<input type="hidden" name="submitted">
</form>
</body>
</html>
<?php
if ($_POST[submitted])
{
$pg=$_POST[graduate];
echo "<pre>";
print_r($pg);
echo "</pre>";
}
?>
ref : www.programmersheaven.com

Thursday, September 11, 2008

zip and stream file with php

I've wrote the function to zip the files and stream out

//$filesToStream : can be array or just file string.
//$dir : directory of file
function zipAndStream($filesToStream,$dir){
  $file = 'tempfile'.rand(9999,1000).".zip";
  $zip = new ZipArchive;
  $res = $zip->open( $file , ZIPARCHIVE::OVERWRITE );
  if ($res === TRUE && file_exists($dir.'/'.$filename) ) {
    if(is_array ($filesToStream)){
      $filecount=0;
      foreach($filesToStream as $filetostream){
        $zip->addFile($dir.'/'.$filetostream,$filetostream);
        $filecount++;
        $filename = substr($filetostream,0,strrpos($filetostream,'.'));
      }
      if($filecount!=1){
        $filename = 'files';
      }
    }else{
      $zip->addFile($filedir.'/'.$filestostream,$filestostream);
      $filename = substr($filestostream,0,strrpos($filestostream,'.'));
    }
    $zip->close();
    header("Content-Type: application/zip");
    header("Content-Length: " . filesize($file));
    header("Content-Disposition: attachment; filename=\"".$filename.".zip\"");
    readfile($file);
  } else {
    listfile();
  }
  unlink($file);
}
to verify that your host server has zip extension try this
if(!extension_loaded('zip'))print_r('

ZIP Extension Is Disable

');