6

WordPressの投稿添付ファイルからzipファイルを作成しようとしています。

以下の両方の方法を試しましたが、何も起こりません(エラーメッセージが表示されず、ファイルが作成されません)-何が間違っていますか(再び..)

それらがwordpressの投稿添付ファイルであるという事実は、それとは何の関係もないと思います-これらのメソッドは通常のファイルでも失敗したためです。なぜ ? ->答えを参照してください。

$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();

どちらの方法でも何も返されませんでした。

編集I- 配列$files_to_zipのサンプルprint_r

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()を使用すると、実際のパスが生成されます(ある時点で、phpがHTTP経由でzipを作成できないのではないかと思いました。これが短い答えです。以下の長いものを参照してください。)

4

1 に答える 1

7

わかりました-私はここで私自身の質問に答えます..

私自身の疑いを確認しました-PHPはHTTPを介して渡されたときにZIPを作成できません-したがって、URLではなくPATHが必要です...

したがって、たとえば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やpdfの動的作成などの画像ライブラリは、すべてHTTPで失敗します。

于 2012-03-28T14:23:14.033 に答える