2016-12-23 3 views
1

Zip скачать выпуск в PHP

if(isset($_REQUEST['create_all'])) 
 
{ 
 
\t $app_id = $_REQUEST['app']; 
 
\t $fullpath = array(); 
 
\t $dir = dirname(__FILE__); 
 
\t 
 
\t 
 
\t foreach($language_array as $key_lang=>$name_lang) 
 
\t { 
 
\t \t $path = $dir."/uploaded_xml/".$app_id."/"; 
 
\t \t 
 
\t \t if(!is_dir($path)) 
 
\t \t { 
 
\t \t \t mkdir($path,0777); 
 
\t \t \t chmod($path,0755); 
 
\t \t } 
 
\t \t 
 
\t \t 
 
\t \t $QUERY=mysql_query("select a.process_flag,a.field_id,a.field_name,a.xml_status,b.*,c.process_flag,b.lang_id from app_lang_fieldmaster_new a left join app_lang_translatetxt_new b on a.field_id=b.field_id left join app_lang_user_status c on a.field_id=c.field_id left join app_wise_lang_fields d on a.field_id=d.field_id where a.process_flag='0' and c.process_flag='6' and b.lang_id='".$key_lang."' and a.xml_status='1' and a.module_master_id='1' and d.app_id='".$app_id."' group by 2",$cont); 
 
\t \t 
 
\t \t 
 
\t \t //create the xml document 
 
\t \t $xmlDoc = new DOMDocument("1.0", "UTF-8"); 
 

 
\t \t //create the root element 
 
\t \t $root = $xmlDoc->appendChild(
 
\t \t \t \t $xmlDoc->createElement("resources")); 
 
\t \t 
 
\t \t while($row=mysql_fetch_object($QUERY)) 
 
\t \t { 
 
\t \t \t if($key_lang==9) 
 
\t \t \t { 
 
\t \t \t \t $xml_string = $xmlDoc->createElement("string",trim($row->translate," ")); 
 
\t \t \t \t $xml_string->setAttribute("name",$row->field_name); 
 
\t \t \t \t $root->appendChild($xml_string); 
 
\t \t \t } 
 
\t \t \t else 
 
\t \t \t { 
 
\t \t \t \t $xml_string = $xmlDoc->createElement("string",trim(base64_decode($row->translate)," ")); 
 
\t \t \t \t $xml_string->setAttribute("name",$row->field_name); 
 
\t \t \t \t $root->appendChild($xml_string); 
 
\t \t \t } 
 
\t \t } 
 

 
\t \t header("content-type: text/plain; charset=ISO-8859-15"); 
 

 
\t \t //make the output pretty 
 
\t \t $xmlDoc->formatOutput = true; 
 
\t \t $string = $xmlDoc->saveXML(); 
 
\t \t $handle = fopen($path.$name_lang.".xml","w"); 
 
\t \t fwrite($handle,$string); 
 
\t \t fclose($handle); 
 
\t \t 
 
\t \t // $string = $xmlDoc->save($path.$name_lang.".xml"); 
 
\t \t 
 
\t \t $fullpath[$key_lang] = $path.$name_lang.".xml"; 
 
\t } 
 
\t 
 
\t 
 
\t ob_clean(); 
 
\t flush(); 
 
\t 
 
\t //zip file creation 
 
\t $zip = new ZipArchive(); 
 
\t 
 
\t //zip file name 
 
\t $FilePath = $app_id.".zip"; 
 
\t 
 
\t $full_path_zip = 'https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$FilePath; 
 
\t 
 
\t //check if file exists if yes then delete 
 
\t if(file_exists(dirname(__FILE__)."/uploaded_xml/".$FilePath)) 
 
\t { 
 
\t \t unlink (dirname(__FILE__)."/uploaded_xml/".$FilePath); 
 
\t } 
 
\t 
 
\t //open zip file to add files to zip file 
 
\t if ($zip->open(dirname(__FILE__)."/uploaded_xml/".$FilePath, ZIPARCHIVE::CREATE) != TRUE) 
 
\t { 
 
\t \t die ("Could not open archive"); 
 
\t } 
 
\t 
 
\t 
 
\t \t //$zip->addFile("file_path","file_name"); 
 

 
\t // loop through each file 
 
\t foreach($language_array as $key=>$name) 
 
\t { 
 
\t \t //add files to zip 
 
\t \t $zip->addFile($fullpath[$key],$name.".xml"); 
 
\t } 
 
\t 
 
\t // close and save archive 
 

 
\t $zip->close(); 
 
\t // header_remove(); 
 
\t echo $full_path_zip; 
 
\t ob_clean(); 
 
\t flush(); 
 
\t $filename = dirname(__FILE__)."/uploaded_xml/".$FilePath; 
 
\t 
 
\t header('Cache-Control: public'); 
 
\t header('Content-Description: File Transfer'); 
 
\t header('Content-type: application/zip'); 
 
\t header('Content-Disposition: attachment; filename="'.$filename.'"'); 
 
\t // echo dirname(__FILE__)."/uploaded_xml/".$FilePath; 
 
\t str_replace(".php",".zip",$filename); 
 
\t $file_content = file_get_contents($filename); // To download the ZIP file 
 
\t print($file_content); \t 
 

 
}

я создаю почтовый файл, который содержит некоторые XML-файлы с сервера, я добавил, что XML-файлы, чтобы пронестись, а затем пытается загрузить молнию, но он загружает .php. Вот мой код

$xmlDoc = new DOMDocument("1.0", "UTF-8"); 

    //create the root element 
    $root = $xmlDoc->appendChild(
       $xmlDoc->createElement("resources")); 

    while($row=mysql_fetch_object($QUERY)) 
    { 
     if($key_lang==9) 
     { 
      $xml_string = $xmlDoc->createElement("string",trim($row->translate," ")); 
      $xml_string->setAttribute("name",$row->field_name); 
      $root->appendChild($xml_string); 
     } 
     else 
     { 
      $xml_string = $xmlDoc->createElement("string",trim(base64_decode($row->translate)," ")); 
      $xml_string->setAttribute("name",$row->field_name); 
      $root->appendChild($xml_string); 
     } 
    } 

    header("content-type: text/plain; charset=ISO-8859-15"); 

    //make the output pretty 
    $xmlDoc->formatOutput = true; 
    $string = $xmlDoc->saveXML(); 
    $handle = fopen($path.$name_lang.".xml","w"); 
    fwrite($handle,$string); 
    fclose($handle); 

    // $string = $xmlDoc->save($path.$name_lang.".xml"); 

    $fullpath[$key_lang] = $path.$name_lang.".xml"; 
} 


ob_clean(); 
flush(); 

//zip file creation 
$zip = new ZipArchive(); 

//zip file name 
$FilePath = $app_id.".zip"; 

$full_path_zip = 'https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".$FilePath; 

//check if file exists if yes then delete 
if(file_exists(dirname(__FILE__)."/uploaded_xml/".$FilePath)) 
{ 
    unlink (dirname(__FILE__)."/uploaded_xml/".$FilePath); 
} 

//open zip file to add files to zip file 
if ($zip->open(dirname(__FILE__)."/uploaded_xml/".$FilePath, ZIPARCHIVE::CREATE) != TRUE) 
{ 
    die ("Could not open archive"); 
} 


    //$zip->addFile("file_path","file_name"); 

// loop through each file 
foreach($language_array as $key=>$name) 
{ 
    //add files to zip 
    $zip->addFile($fullpath[$key],$name.".xml"); 
} 

// close and save archive 

$zip->close();` 

вот мой скачать почтовый файл

$file_content = file_get_contents(dirname(__FILE__)."/uploaded_xml/".$FilePath); // To download the ZIP file 
header('Cache-Control: public'); 
header('Content-Description: File Transfer'); 
header('Content-type: application/force-download'); 
header('Content-Disposition: attachment; filename="'.dirname(__FILE__)."/uploaded_xml/".$FilePath.'"'); 
echo $file_content; 

он загружает .php файл со следующим содержанием

PK ”R—I+ Qý  
Bulgarian.xml­”ÁnÓ@†ï}Š•ïP¸õ¤B J•Z‰pµ{’X²×ÑzàR ¢¤ŠBHo`ª˜†¤q_aöø×NÔÕ*nkÏÎ73ÿÌlk÷U‰—¤Ò0‘mçáýŽ é'A(ûmçù³½{;Îng«¥(M2åSÚÙ¢•j»^Lm§§B’AêFÝÈ]žå’§fÌ…à+3Âñ£ýà9O[Ûµ{3i˜E‘«WQq–â'32'|Å.jî‚sž ÌÜ raÞØß%‡©äóM¡ü§«Œ»*ñßK5"|æÀNÌ1|s§Ù¨(³Ø푧3EnªÉ‹ôà0  ou­f,PzÁ?xʗͬHö‡iêÊDº²ïõÉ•®ï©JÅ3ÔqÁPr[ÏRÂOÏ8‡ cü<ÆyC©ûÒû/€’ÇÑA¥ä©yî-ù°êà°àíªµô*TèGYp¸¸ËŒó†…íË?H2ýt iÅGƒÖ#üú]~ÇßQ•Ã4XOúÝ+±GOŒl£,vnsÌÊä/øHû6<f‚/ïš6°÷šâÜsä8Z-¦ÔŸ<ß°WX€Úÿ‹²ù"IMêúö)V±lÚu¬Ùf÷=ϧ'¤É׉:ðºT=µ/ϬÄf$°9¹°/Žy_YÊk\k{íAûPK ”R—I„üa[€ Î  Catalan.xml“ËN+1†÷<E”=—³c1„¸HH ±èa¹‰gj)—ÊI*x(6‡Gè‹áiT!¦=›(·ï÷Ûi®^ƒWkäL)ÎôŸ³­0Úä(ö3ýw~z©¯Ú“†1§Ês{¢T“˹Šp¦;&Œ.¿ðfœëöÎg°flnÎwÄ4¼ªÞ›’c'¡–ƒ˜nçÄÀ 
Öà½Z!«Ý¡žÖ³K(['Nà,ä¢Û[êj¦ÍÇ4µbTƒéJe4¹ ... 

Если я изменить расширение загруженного файла с. php to .zip работает нормально.

Пожалуйста, помогите заранее спасибо ...

+0

ReadFile ("$ archive_file_name"); после заголовков insted print_r()/echo и попробуйте один раз. –

ответ

2

Удалить эту строку: echo $file_content;

// To download the ZIP file 
header('Cache-Control: public'); 
header('Content-Description: File Transfer'); 
header('Content-type: application/force-download'); 
header('Content-Disposition: attachment; 
filename="'.dirname(__FILE__)."/uploaded_xml/".$FilePath.'"'); 
+0

не загружается ... – user7334323

+0

@ user7334323, ** readfile ("$ archive_file_name"); ** после заголовков insted print_r()/echo и попробовать один раз. –

0

Попробуйте

$file_content = file_get_contents(dirname(__FILE__)."/uploaded_xml/".$FilePath); // To download the ZIP file 
header('Cache-Control: public'); 
header('Content-Description: File Transfer'); 
header('Content-type: application/force-download'); 
header('Content-Disposition: attachment; filename="'.dirname(__FILE__)."/uploaded_xml/".$FilePath.'"'); 
+0

все еще не вынуждает скачать ... – user7334323

+0

удалил echo $ file_content; –

+0

echo this dirname (__ FILE __). "/ Uploaded_xml /".$ FilePath. –

Смежные вопросы