在运行查询之前检查文件是否存在

时间:2014-06-25 23:25:48

标签: php mysql if-statement

我是新手,请原谅我的无知;

我有一个上传的PDF表单,我检查表单是否已经存在,如果没有,我上传它,因此想要运行mysql插入查询。我试过这个:

if (file_exists($pdfSubDirectory.$filename))
    if (isset($_POST['submit']))
    {
      $quer->execute();
    }

但是我得到了错误;

>致命错误:在非对象上调用成员函数execute()

任何想法?

我的主要代码是;

    <?php
require "config.php";

$cat        = $_GET['cat1'];
$catid      = $_GET['cat'];
$subcat     = $_GET['subcat'];
$subcatid   = $_GET['subcatid'];
$subcat2    = $_GET['subcat2'];

//form vars
$form_class = $_POST['Class']; // create modify delete flag
$form_cerfa = $_POST['Cerfa']; // cerfa Yes or No flag
$form_id = $_POST['ID'];
$form_name = $_POST['Name'];

$predir = "./docs";
$dir = $predir."/" .$cat. "/" .$subcat."/" .$subcat2 . "/";
$dir= strtolower($dir);
$dir= str_replace(' ', '_', $dir);


$MyHtml1 = <<<EOD

<!doctype html>
<html>
<head>
    <title>$subcat / $subcat2</title>
</head>
<body>
<table align="center" border="0" cellpadding="1" cellspacing="1" style="height: 100%; width: 100%;">
    <tbody>
        <tr>
            <td>
            <table border="0" cellpadding="0" cellspacing="0" style="height: 100%; width: 100%;">
                <tbody>
                    <tr>
                        <td style="width: 50%;">
                        <table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">
                            <tbody>
                                <tr>
                                    <td style="width: 50%;">You are in the category:</td>
                                    <td style="width: 50%;">$cat</td>
                                </tr>
                                <tr>
                                    <td>You are in the subcategory:</td>
                                    <td>$subcat</td>
                                </tr>
                                <tr>
                                    <td>You are in section:</td>
                                    <td>$subcat2</td>
                                </tr>
                            </tbody>
                        </table>
                        </td>
                        <td>
                        <p>Time:</p>

                        <p>Date:</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        <hr />
                        <p>...</p>
                        </td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>

EOD;

echo $MyHtml1;
$pdfDirectory = $dir;
$pdfSubDirectory = $pdfDirectory.$_POST['Class']."/";
$thumbDirectory = $pdfDirectory . "pdfimage/";
$filename = basename( $_FILES['pdf']['name'], ".pdf");
$filename = preg_replace("/[^A-Za-z0-9_-]/", "", $filename).".pdf";
$thumb = basename($filename, ".pdf");

if (file_exists($pdfSubDirectory.$filename)) {
    echo "This file already exists.";
} else {
    if(move_uploaded_file($_FILES['pdf']['tmp_name'], $pdfSubDirectory.$filename)) {

    //the path to the PDF file
    $pdfWithPath = $pdfDirectory.$filename;
    $pdfWithSubPath = $pdfSubDirectory.$filename;
    $pdfWithPathImage = $pdfDirectory."pdfimage/";
    $pdfWithPathImageThumb = $pdfWithPathImage.$thumb.".jpg";
    //add the desired extension to the thumbnail
    $thumb = $thumb.".jpg";

    //execute imageMagick's 'convert', setting the color space to RGB and size to 200px wide
    //exec("convert \"{$pdfWithSubPath}[0]\" -colorspace RGB -geometry 100 $thumbDirectory$thumb");
    exec("convert  -density 200 -page 792x612 \"{$pdfWithSubPath}[0]\" -append -resize 240 -background white -flatten +repage $thumbDirectory$thumb");   
    //show the image
    echo "<p><a href=\"$pdfWithPath\"><img src=".$pdfWithPathImageThumb." alt=\"\" /></a></p>";
    echo $pdfWithPathImageThumb;
        }
}
if (isset(file_exists($pdfSubDirectory.$filename)))
    {
      $quer->execute();
    }



///////// start of query for form addition//////////
$quer = $dbo -> prepare("INSERT into form_detail(form_id,form_name,form_category,form_subcategory,form_subcategory2,form_class,form_cerfa) VALUES(:form_id,:form_name,:form_category,:form_subcategory,:form_subcategory2,:form_class,:form_cerfa)");
$quer ->bindParam(':form_id', $form_id);
$quer ->bindParam(':form_name', $form_name);
$quer ->bindParam(':form_category', $catid);
$quer ->bindParam(':form_subcategory', $subcatid);
$quer ->bindParam(':form_subcategory2', $subcat2);
$quer ->bindParam(':form_class', $form_class);
$quer ->bindParam(':form_cerfa', $form_cerfa);

///////////// End of query for form addition////////////


$MyHtml2 = <<<EOD

<form action="" method="post" enctype="multipart/form-data" name="MyForm">
        <input type="file" name="pdf" /><br>

           <table border="0" cellpadding="0" cellspacing="0" style="width: 500px;">
    <tbody>
        <tr>
            <td><label>Cerfa : </label></td>
            <td><input type="radio" name="Cerfa" value="1" checked="checked"/>Yes<input type="radio" name="Cerfa" value="0"/>No<br/></td>
        </tr>
        <tr>
            <td><label>Form Class: </label></td>
            <td><select name="Class">
            <option value="create">Create</option>
            <option value="modify">Modify</option>
            <option value="delete">Delete</option>
        </select></td>
        </tr>
        <tr>
            <td> <label>Form Name: </label></td>
            <td><input type="text" name="Name" value="Name"/><br></td>
        </tr>
        <tr>
            <td><label>Form Reference No : </label></td>
            <td><input type="text" name="ID" value="Reference Number"/></td>
        </tr>
        <tr>
            <td>Check details and upload</td>
            <td><input type="submit" name="submit" value="Upload"/></td>
        </tr>
    </tbody>
</table>



</form>

<table border="0" cellpadding="0" cellspacing="0" style="width: 937px;">
    <tbody>
        <tr>
            <td colspan="2">
            <p>Forms Available</p>
            </td>
        </tr>
    </tbody>
</table>

<table border="1" cellpadding="0" cellspacing="0" style="height: 44px; width: 100%;">
    <tbody>
        <tr>
            <td style="vertical-align: middle; width: 228px;">Ref</td>
            <td rowspan="1" style="vertical-align: middle; width: 89px;">Cerfa</td>
            <td colspan="3" rowspan="1" style="vertical-align: middle; width: 595px;">Name</td>
        </tr>
        <tr>
            <td style="vertical-align: middle; width: 228px;">&nbsp;</td>
            <td rowspan="1" style="text-align: center; vertical-align: middle;">&nbsp;</td>
            <td colspan="3" rowspan="1" style="text-align: center; vertical-align: middle;">&nbsp;</td>
        </tr>
    </tbody>
</table>

<p>&nbsp;</p>
</body>
</html>

EOD;

echo $MyHtml2;

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir."create/"));

while($it->valid()) {

    if (!$it->isDot()) {

        echo 'File Name: ' . $it->getSubPathName() . "\n<br>";
    }

    $it->next();
}
?>

1 个答案:

答案 0 :(得分:1)

您需要在执行前准备$quer。把这段代码......

///////// start of query for form addition//////////
$quer = $dbo -> prepare("INSERT into form_detail(form_id,form_name,form_category,form_subcategory,form_subcategory2,form_class,form_cerfa) VALUES(:form_id,:form_name,:form_category,:form_subcategory,:form_subcategory2,:form_class,:form_cerfa)");
$quer ->bindParam(':form_id', $form_id);
$quer ->bindParam(':form_name', $form_name);
$quer ->bindParam(':form_category', $catid);
$quer ->bindParam(':form_subcategory', $subcatid);
$quer ->bindParam(':form_subcategory2', $subcat2);
$quer ->bindParam(':form_class', $form_class);
$quer ->bindParam(':form_cerfa', $form_cerfa);
///////////// End of query for form addition////////////

高于此:

if (file_exists($pdfSubDirectory.$filename))
    if (isset($_POST['submit']))
    {
      $quer->execute();
    }

当脚本运行时,它会转到if (file_exists($pdfSubDirectory.$filename)),发现它是真的,并尝试执行一个不存在的查询。