douzhushen_9776
2014-06-25 23:25
浏览 43
已采纳

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

I am a newbie so forgive my ignorance;

I have an upload PDF form that I check if the form already exists, if it doesnt, I upload it and would therefore like to run a mysql insert query. I tried this:

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

but I get the error;

> Fatal error: Call to a member function execute() on a non-object

any ideas?

my main code is;

    <?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() . "
<br>";
    }

    $it->next();
}
?>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duangong0690 2014-06-25 23:32
    已采纳

    You need to prepare $quer before executing it. Put this code...

    ///////// 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////////////
    

    Above this:

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

    When the script is ran, it gets to the if (file_exists($pdfSubDirectory.$filename)), finds it true, and tries to execute a query that doesn't exist.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题