0

I have an error in my code. I'm uploading multiple images using an HTML/PHP forum. But when I want them to show in my FPDF I get this error:

Warning: getimagesize(images/IMG_20181017_115937.jpg): failed to open stream: No such file or directory in C:\wamp64\www\Petrogas NCR applicatie\FPDF\fpdf.php

This is my code:

if (mysqli_query($conn, $sql)) {

    $query = "SELECT id FROM ncr_input ORDER BY id DESC LIMIT 1";
    $result = mysqli_query($conn, $query);

    $row = mysqli_fetch_row($result);

    $ncrnummer = $row[0];

    for($i = 0; $i < count($_FILES['attachments']['name']); $i++)
    {
        $filetmp = $_FILES["attachments"]["tmp_name"][$i];
        $filename = $_FILES["attachments"]["name"][$i];
        $filepath = "images/".$filename;

        move_uploaded_file($filetmp,$filepath);

        $querypictureupload = "INSERT INTO file_upload (ncrnummer,filename) VALUES ('$ncrnummer','$filename')";
        $result = mysqli_query($conn, $querypictureupload);
    }

    ob_start();
    $pdf = new FPDF();

    $pdf->AddPage();

    $pdf->Rect(10, 10, 190, 186, 'D');

    $pdf->Image("sitephotos/Petrogaslogo.jpg",11,11,35,17.5,"JPG");

    $pdf->SetFont("Helvetica", "B", 15);
    $pdf->Cell(0,20, "{$typencr}",1,1, 'C');
    $pdf->SetFont("Arial", "B", 10);

    $pdf->Cell(47.5,10,"Brief description:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$categorie}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Drawingnr:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$tekeningnr}", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"P.G. reference:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$projectnummer}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Contact person:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$projectleider}", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Project description:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$projectnaam}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Department:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$waargeconstateerd}", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(190,10,"Description of Non - Conformity / Complaint:", 1 ,1);
    $pdf->SetFont("Arial", "", 8);
    $pdf->MultiCell(190,4, "{$omschrijvingprobleem}\n\n",0,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(190,10,"Analysis of the cause of Non - Conformity / Complaint:", 1 ,1);
    $pdf->SetFont("Arial", "", 8);
    $pdf->MultiCell(190, 4, "{$omschrijvingoorzaak}\n\n",0,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Deviation caused by:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$veroorzaaktdoor}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Supplier:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$klant}", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Report by:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$gerapporteerddoor}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Date:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"-", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Proposed disposition:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$correctiemaatregelen}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Planned completion:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$geplandedatumgereed}", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Action by:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$corractiedoor}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Estimated costs/hrs:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$geschattekosten} / {$geschatteuren}", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(190,10,"Approval QM     ........                         Approval others     ........                         Acceptance AI     ........", 1 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Identify corrective action", 1 ,0);
    $pdf->SetFont("Arial", "", 12);
    $pdf->Cell(47.5,10,"String maken......", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(190,10,"Reviewed by:                                                                                       Review date:", 1 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(190,10,"Verification QM     ........                         Verification others     ........                         Acceptance AI     ........", 1 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Preventive action:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$preventieveacties}", 0 ,1);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Status:", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"{$status}", 0 ,0);

    $pdf->SetFont("Arial", "B", 10);
    $pdf->Cell(47.5,10,"Date closed", 1 ,0);
    $pdf->SetFont("Arial", "", 8);
    $pdf->Cell(47.5,10,"-", 0 ,1);

    $pdf->AddPage();

    $query1 = "SELECT filename FROM `file_upload` WHERE ncrnummer = '$ncrnummer'";
    $result1 = mysqli_query($conn, $query1);

    while ($row = mysqli_fetch_assoc($result1)) {
        $image = $row['filename'];
        $pdf->Image("images/".$image,NULL , NULL ,100,85, "");
        $pdf->Cell(190, 3, "", 0, 1);
    }

    $pdfdoc = $pdf->Output('', 'S');
    ob_end_flush();

    $mail = new PHPMailer();

    $mail->SMTPDebug = 0;

    $mail->Host = "";
    $mail->isSMTP();
    $mail->SMTPAuth = "";
    $mail->Username = "";
    $mail->Password = "";
    $mail->Port = 25;

    $mail->addAddress($projectleideremail);
    $mail->setFrom('');
    $mail->Subject = "NIEUWE NCR Project: {$projectnummer}";
    $mail->isHTML(true);
    $mail->Body = "DIT IS EEN TEST NCR";
    $mail->addStringAttachment($pdfdoc, 'NCR.pdf');

    if ($mail->send()) {
        echo "<script>alert('Uw e-mail is verstuurd met een attachment')</script>";
    }

Is there somebody familiar with FPDF error codes?

DarkSuniuM
  • 2,523
  • 2
  • 26
  • 43
Rob
  • 13
  • 5
  • I think it has nothing to do with FPDF. It just uses the standard function *getimagesize()*. The errror is quite obvious. You should check the correct path of your images, further it is besser to work with absolute paths. – BenRoob Oct 17 '18 at 10:35
  • 1
    Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly Oct 17 '18 at 10:39
  • @BenRoob do you mean with an absolute path, C: \ wamp64 \ www \ Petrogas NCR application \ images? – Rob Oct 17 '18 at 10:42
  • @RiggsFolly the application will be used at a local server......i'm just a student so still learning on SQL injections – Rob Oct 17 '18 at 10:44
  • An absolute path like that should avoid your script is looking for a relative path in the FPDF folder (see the warning message). – BenRoob Oct 17 '18 at 10:45
  • I assume you did the obvious check and made sure a file actually exists in `images/IMG_20181017_115937.jpg` – RiggsFolly Oct 17 '18 at 10:47
  • @RiggsFolly I've checked now......and it does not exist – Rob Oct 17 '18 at 10:52
  • Well now you know where the problem is – RiggsFolly Oct 17 '18 at 10:53
  • @RiggsFolly it uploads when you select just one image......but when you select multiple you get that message:Warning: getimagesize(images/IMG_20181017_115933.jpg): failed to open stream: No such file or directory in C:\wamp64\www\Petrogas NCR applicatie\FPDF\fpdf.php on line 1240 – Rob Oct 17 '18 at 10:56
  • Is there maybe something wrong with my For loop? for uploading the images? – Rob Oct 17 '18 at 10:58
  • I also note you never check for `$_FILES["attachments"][$i]['error']` maybe you hit the `upload_max_filesize` or `post_max_size` limits when uploading more than one file – RiggsFolly Oct 17 '18 at 11:00
  • Have you looked at your `php error log`? – RiggsFolly Oct 17 '18 at 11:00
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Oct 17 '18 at 11:01
  • _“Is there maybe something wrong with my For loop?”_ - don’t ask us - debug it! Check for upload errors, as Riggs just said. Check the return value of `move_uploaded_file`, that will tell you whether it was actually able to move the file, or if something went wrong, etc. … – misorude Oct 17 '18 at 11:01
  • @RiggsFolly do you know the max upload size....? My 3 images together are 6MB.....is that too much? – Rob Oct 17 '18 at 11:03
  • It is controlled from your `php.ini` file look for the parameter names I gave in previous comment – RiggsFolly Oct 17 '18 at 11:05

0 Answers0