无法编译超过2的PDF

时间:2015-09-29 05:28:07

标签: php pdf file-upload

我还在学习PHP编码,现在,我遇到了一个问题。这是一个PHP编码,可将最多四个PDF文件编译成一个PDF,但无论我放入多少文件,都只能编译2个文件。任何人都可以帮我看看问题出在哪里,因为我需要很快通过这个项目。 如果有人告诉我应该改变的地方,这将是一个很大的帮助。

以下是编码:

<?php 

include 'PDFMerger.php';

$pdf = new PDFMerger;

$uploaddir = "upload/uploaded/";  //set this to where your files should be uploaded.  
$i = 1;
$j = 1;

if (isset($_FILES['file'])) { 


    foreach($_FILES['file']['type'] as $key => $value) { 

    $ispdf = end(explode(".",$_FILES['file']['name'][$key]));  //make sure it's a PDF file     
    $ispdf = strtolower($ispdf); 

        if ($value && $ispdf=='pdf') { 
            //upload each file to the server 
            $filename = $i . '.' . 'pdf'; 
            $filename = str_replace(" ","",$filename); //remove spaces from file name 
            $uploadfile = $uploaddir . $filename; 
            move_uploaded_file($_FILES['file']['tmp_name'][$key], $uploadfile); 
            $i++;
            $j++; 
        } 

    } 

    if ($j = 2) {
        goto first;
    }

    else {
        if ($j = 3) {
           goto second;
        }

        else {
        goto third;
        }
    }   




first:  
$pdf->addPDF('upload/uploaded/1.pdf')
    ->addPDF('upload/uploaded/2.pdf')
    ->merge('download', 'compiler.pdf');
    goto end;   

second:
$pdf->addPDF('upload/uploaded/1.pdf')
    ->addPDF('upload/uploaded/2.pdf')
    ->addPDF('upload/uploaded/3.pdf')
    ->merge('download', 'compiler.pdf');
    goto end;   

third:
$pdf->addPDF('upload/uploaded/1.pdf')
            ->addPDF('upload/uploaded/2.pdf')
            ->addPDF('upload/uploaded/3.pdf')
            ->addPDF('upload/uploaded/4.pdf')
            ->merge('download', 'compiler.pdf');
        goto end;   
}

end:
$myFile = "upload/uploaded/1.pdf";
unlink($myFile);

$myFile = "upload/uploaded/2.pdf";
unlink($myFile);

$myFile = "upload/uploaded/3.pdf";
unlink($myFile);

$myFile = "upload/uploaded/4.pdf";
unlink($myFile);

?> 

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

for($n = 1; $n =< $j; $n++){
    $pdf->addPDF('upload/uploaded/' . $n . '.pdf');
}
$pdf->merge('download', 'compiler.pdf');

忽略整个if($j == 2){}等块。