jeudi 19 novembre 2020

Convert PDF to image add to database with pdo statement

I am actually working on a script to add a PDF and is thumbnail to the database. But misfortune, I cannot add both to database. Its one or the other? I dont know how to add two variables with one statement.

Here is the complete code:

if(isset($_POST['form2'])) {
    $valid = 1;
    
    $path=$_FILES['photo']['name'];
    $path_tmp=$_FILES['photo']['tmp_name'];

    if($path!='') {
        $ext = pathinfo( $path, PATHINFO_EXTENSION );
        $file_name = basename( $path, '.' . $ext );
        if( $ext!='pdf') {
            $valid = 0;
            $error_message .= 'You must have to upload pdf file<br>';
        }
    }

    if($valid == 1) {

        if($path=='') {
            // updating the database
            $statement = $pdo->prepare("UPDATE tbl_advertisement SET adv_edition=?, adv_prochain=?, adv_url=?, adv_status=? WHERE adv_id=2");
            $statement->execute(array($_POST['adv_edition'],$_POST['adv_prochain'],$_POST['adv_url'],$_POST['adv_status']));
        } else {

            $image = $path;

            move_uploaded_file($path_tmp,"../Archives/pdf/".$path);

            //remove all characters from the file name other than letters, numbers, hyphens and underscores
            $withoutExt = preg_replace('/\.' . preg_quote(pathinfo($image, PATHINFO_EXTENSION), '/') . '$/', '', $image);

            //add the desired extension to the thumbnail
            $thumb = $withoutExt."Mini.jpg";
            
            //the path to the PDF file
            $pdfWithPath = "../Archives/pdf/".$path;

            $thumbDirectory = "../Archives/pdfimages/";

            //execute imageMagick's 'convert', setting the color space to RGB and size to 200px wide
            exec("convert \"{$pdfWithPath}[0]\" -colorspace RGB -geometry 500 $thumbDirectory$thumb");

            // updating the database
            $statement = $pdo->prepare("UPDATE tbl_advertisement SET adv_photo=?, adv_pdf=?, adv_url=?, adv_status=? WHERE adv_id=2");
            $statement->execute(array($image,$_POST['edition_photo'],$_POST['adv_url'],$_POST['adv_status']));
        }

        $success_message = 'Advertisement is updated successfully.';
    }


}

The code is interesting but not working yet

Aucun commentaire:

Enregistrer un commentaire