สอบถามเรื่อง miltiple file download หน่อยครับ

ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา สอบถามเรื่อง miltiple file download หน่อยครับ

สอบถามเรื่อง miltiple file download หน่อยครับ

<?php
	$error = "";		//error holder
	if(isset($_POST['btnDownload'])){
		$post = $_POST;		
		$file_folder = "application_download/";	// folder to load files
		if(extension_loaded('zip')){	// Checking ZIP extension is available
			if(isset($post['files']) and count($post['files']) > 0){	// Checking files are selected
				$zip = new ZipArchive();			// Load zip library	
				$zip_name = time().".zip";			// Zip name
				if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){		// Opening zip file to load files
					$error .=  "* Sorry ZIP creation failed at this time<br/>";
				}
				foreach($post['files'] as $file){				
					$zip->addFile($file_folder.$file);			// Adding files into zip
				}
				$zip->close();
				if(file_exists($zip_name)){
					// push to download the zip
					header('Content-type: application/zip');
					header('Content-Disposition: attachment; filename="'.$zip_name.'"');
					readfile($zip_name);
					// remove zip file is exists in temp path
					unlink($zip_name);
				}
				
			}else
				$error .= "* Please select file to zip <br/>";
		}else
			$error .= "* You dont have ZIP extension<br/>";
	}
?>
<html>
<head>
<title>ThaiCreate.Com PHP & MySQL Tutorial</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<script language="JavaScript">
	function ClickCheckAll(vol)
	{
	
		var i=1;
		for(i=1;i<=document.frmMain.hdnCount.value;i  )
		{
			if(vol.checked == true)
			{
				eval("document.frmMain.files" i ".checked=true");
			}
			else
			{
				eval("document.frmMain.files" i ".checked=false");
			}
		}
	}
</script>
<form name="frmMain" action="" method="post">
<?php if(!empty($error)) { ?>
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p>
<?php } ?>
<?
$objConnect = mysql_connect("localhost","root","password") or die("Error Connect to Database");
$objDB = mysql_select_db("checkall2");
$strSQL = "SELECT * FROM tbl_application ORDER BY id ASC limit 0,5";
mysql_query("SET NAMES UTF8");
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
?>
<table width="600" border="1">
  <tr>
    <th width="10"> <div align="center">
      <input name="CheckAll" type="checkbox" id="CheckAll" value="Y" onClick="ClickCheckAll(this);">
    </div></th>
    <th width="10"> <div align="center">id </div></th>
    <th width="120"> <div align="center">regis_id </div></th>
    <th width="300"> <div align="center">project_name </div></th>
    <th width="100"> <div align="center">duedate </div></th>
    <th width="50"> <div align="center">file </div></th>
  </tr>
<?
$i = 0;
while($objResult = mysql_fetch_array($objQuery))
{
$i++;
?>
  <tr>
    <td align="center"><input type="checkbox" name="files[]" id="files<?=$i;?>" value="application_download/<?php echo $objResult['file'] ;?>"></td>
    <td><div align="center"><?=$objResult["id"];?></div></td>
    <td><?=$objResult["regis_id"];?></td>
    <td><?=$objResult["project_name"];?></td>
    <td align="center"><?=$objResult["duedate"];?></td>
    <td align="center"><a href="application_download/<?php echo $objResult['file'] ;?>"><img src="pdf-icon.png" width="24" height="24" border="0" alt=""></a></td>
  </tr>
<?
}
?>
</table>
<?
mysql_close($objConnect);
?>
<input type="submit" name="btnDownload" value="Download as ZIP">
<input type="hidden" name="hdnCount" value="<?=$i;?>">
</form>
</body>
</html>

แต่มันไม่มีค่าอะไรออกมาเลยครับ ไม่ทราบว่าต้องแก้ตรงบรรทัดไหนครับพี่


Pusirinon 110.170.246.xxx 07-09-2015 16:14:07

คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )

 ความคิดเห็นที่ 1
ปกติโค้นี้ทำงานได้ไหม ถ้าปกติทำงานได้ ลองลบตรง unlink() ออกก่อนดู


ninenik 1.47.68.xxx 07-09-2015
 ความคิดเห็นที่ 2
จากเดิมผมทำเป็น download ทีละไฟล์ แต่ทีนี้เอามาใส่กับ checkbox ที่เป็น array แล้วมันไม่ทำงานครับ


pusirinon 171.96.168.xxx 07-09-2015 20:36
 ความคิดเห็นที่ 3
ผมเอา Code นี้มาประยุกต์ครับ
<?php
	$error = "";		//error holder
	if(isset($_POST['createpdf'])){
		$post = $_POST;		
		$file_folder = "files/";	// folder to load files
		if(extension_loaded('zip')){	// Checking ZIP extension is available
			if(isset($post['files']) and count($post['files']) > 0){	// Checking files are selected
				$zip = new ZipArchive();			// Load zip library	
				$zip_name = time().".zip";			// Zip name
				if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){		// Opening zip file to load files
					$error .=  "* Sorry ZIP creation failed at this time<br/>";
				}
				foreach($post['files'] as $file){				
					$zip->addFile($file_folder.$file);			// Adding files into zip
				}
				$zip->close();
				if(file_exists($zip_name)){
					// push to download the zip
					header('Content-type: application/zip');
					header('Content-Disposition: attachment; filename="'.$zip_name.'"');
					readfile($zip_name);
					// remove zip file is exists in temp path
					unlink($zip_name);
				}
				
			}else
				$error .= "* Please select file to zip <br/>";
		}else
			$error .= "* You dont have ZIP extension<br/>";
	}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download As Zip</title>
</head>
<body>
<center><h1>Create Zip</h1></center>
<form name="zips" method="post">
<?php if(!empty($error)) { ?>
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p>
<?php } ?>
<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;">
  <tr>
    <td width="33" align="center">*</td>
    <td width="117" align="center">File Type</td>
    <td width="382">File Name</td>
  </tr>
  <tr>
    <td align="center"><input type="checkbox" name="files[]" value="flowers.jpg" /></td>
    <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
    <td>flowers.jpg</td>
  </tr>
  <tr>
    <td align="center"><input type="checkbox" name="files[]" value="fun.jpg" /></td>
    <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
    <td>fun.jpg</td>
  </tr>
  <tr>
    <td align="center"><input type="checkbox" name="files[]" value="9lessons.docx" /></td>
    <td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td>
    <td>9lessons.docx</td>
  </tr>
  <tr>
    <td align="center"><input type="checkbox" name="files[]" value="9lessons.pdf" /></td>
    <td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
    <td>9lessons.pdf</td>
  </tr>
  <tr>
    <td colspan="3" align="center">
    	<input type="submit" name="createpdf" style="border:0px; background-color:#800040; color:#FFF; padding:10px; cursor:pointer; font-weight:bold; border-radius:5px;" value="Download as ZIP" />&nbsp;
        <input type="reset" name="reset" style="border:0px; background-color:#D3D3D3; color:#000; font-weight:bold; padding:10px; cursor:pointer; border-radius:5px;" value="Reset" />
    </td>
    </tr>
</table>

</form>
</body>
</html>


pusirinon 171.96.168.xxx 07-09-2015 20:48
 ความคิดเห็นที่ 4
<input type="checkbox" name="files[]" id="files<?=$i;?>" value="application_download/<?php echo $objResult['file'] ;?>">

ค่าของไฟล์ที่เลือก ส่งมีต่อโฟลเดอร์ กับ ชื่อไฟล์ไปแล้ว

แต่ตอนเรียกใ้ห้ add ไฟล์ ทำไมถึงต่อ โฟลเดอร์อีก ถ้าแบบนั้น ตำแหน่งไฟล์ก็ไม่น่าจะถูกต้อง

  1. foreach($post['files'as $file){                 
  2.                     $zip->addFile($file_folder.$file);           // Adding files into zip  
  3.                 }  

คงไม่ต้องต่อโฟลเดอร์อีกเพราะส่งมาแล้ว น่าจะแก้เป็น

$zip->addFile($file); 


ninenik 1.47.9.xxx 07-09-2015
 ความคิดเห็นที่ 5
ขอบคุณมากครับพี่นิก


pusirinon 171.96.168.xxx 07-09-2015 22:04
1






เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ