2012-03-28 18 views
5

मैं वर्डप्रेस में पोस्ट अटैचमेंट से ज़िप फ़ाइल बनाने की कोशिश कर रहा हूं।PHP ज़िप फ़ाइल (पोस्ट अनुलग्नक वर्डप्रेस से)

मैं दोनों तरीकों से नीचे की कोशिश की है - लेकिन यह कुछ भी नहीं में परिणाम (कोई त्रुटि संदेश, कोई फ़ाइल बनाई) - क्या मुझे नहीं लगता कि तथ्य यह है कि उन है कर रहा हूँ मैं गलत कर (फिर से ..)

वर्डप्रेस में पोस्ट अनुलग्नक हैं इसके साथ कुछ भी करना है - क्योंकि वे विधियां सामान्य फ़ाइलों के साथ भी विफल रही हैं। क्यूं कर ? -> उत्तर देखें।

$files_to_zip = array();// create files array 
    //run a query 
    $post_id = get_the_id(); 
    $args = array(
     'post_type' => 'attachment', 
     'numberposts' => null, 
     'post_status' => null, 
     'post_parent' => $post_id 
    ); 

    $attachments = get_posts($args); 
    if ($attachments) { 
foreach ($attachments as $attachment) { 
     $files_to_zip [] = wp_get_attachment_url($attachment->ID); // populate files array 
     } 
    } 
    print_r($files_to_zip); 
    $zip = new ZipArchive; 
    $zip->open('file.zip', ZipArchive::CREATE); 
    foreach ($files_to_zip as $file) { 
     $zip->addFile($file); 
    } 
    $zip->close(); 

है और यह भी इस विधि:

$files_to_zip = array(); // create array 
//run a query 
$post_id = get_the_id(); 
$args = array(
    'post_type' => 'attachment', 
    'numberposts' => null, 
    'post_status' => null, 
    'post_parent' => $post_id 
); 
$zip = new ZipArchive; 
$zip->open('file.zip', ZipArchive::CREATE); 
$attachments = get_posts($args); 
if ($attachments) { 
    foreach ($attachments as $attachment) { 
    $zip->addFile(wp_get_attachment_url($attachment->ID)); 
    } 
} 

print_r($files_to_zip);// debug - return file names OK 

$zip->close(); 

दोनों ही तरीकों से कुछ भी नहीं लौटे .. किसी भी अंतर्दृष्टि की सराहना की जाएगी।

संपादित मैं - सरणी $ files_to_zip

print_r($files_to_zip); 

Array ( 
[0] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-62316IMAG0659.jpg 
[2] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IGP0255.jpg 
[3] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IZTP0635.jpg 
[4] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_ITG035t5.jpg 
[5] => http://localhost/testing_evn/wp-content/uploads/2012/03/wrt-85520_IRTT7375.jpg) 

get_attached_file() यह असली पथ का उत्पादन करेगा का उपयोग कर ..by के लिए नमूना print_r (कुछ बिंदु पर मैं शक था कि हो सकता है php जिप से अधिक नहीं बना सकते HTTP -। और उस संक्षिप्त उत्तर है नीचे लंबे एक देखें)

+1

कोई create PHP फ़ंक्शन नहीं है जिसे 'create_zip()' कहा जाता है - कृपया इस फ़ंक्शन के लिए कोड दिखाएं। साथ ही, कृपया 'var_dump ($ files_to_zip)' – DaveRandom

+0

का परिणाम दिखाएं नमूना सरणी के लिए मेरा संपादन देखें। –

उत्तर

4

ठीक है -। मैं यहां अपने ही सवाल का जवाब होगा ..

मैं अपने ही सु की पुष्टि की है spicions - पीएचपी ज़िप जब HTTP पर पारित नहीं बना सकते हैं - तो हम एक पथ और नहीं एक यूआरएल की आवश्यकता है ...

तो Wordpress मामले में उदाहरण के लिए, get_attached_file() का उपयोग करने के वास्तविक पथ उत्पादन की जरूरत है ..

Array ( 
[0] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-62316IMAG0659.jpg 
[2] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IGP0255.jpg 
[3] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IZTP0635.jpg 
[4] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_ITG035t5.jpg 
[5] => C:\Documents and Settings\OB\htdocs\test_env\wp-content\uploads\2012\03\wrt-85520_IRTT7375.jpg) 

(var_dump सरणी मैं वास्तव में इस पर कई बार देखा था देखने के बारे में उसकी टिप्पणी के लिए धन्यवाद @DaveRandom, लेकिन जब तक किसी को विशेष रूप से यह देखने के लिए कहा मैं बहुत ध्यान नहीं दिया।)

तो यह मुझे बनाया मुझे एक और समस्या याद है जो मैंने बहुत समय पहले gdlib के साथ किया था - PHP स्ट्रीम फ़ंक्शंस के बारे में, फाइलें बनाना - और HTTP। उदाहरण के लिए gdlib, या पीडीएफ गतिशील निर्माण जैसे छवि पुस्तकालय वे सभी HTTP पर विफल हो जाते हैं।

संबंधित मुद्दे