使用php和mysql上传图片

时间:2017-02-05 16:19:20

标签: php mysql

我正在建立一个上传页面,允许我上传图片,记录将存储在MySQL中。现在,我能够保存记录。但是,我遇到了将图片存储在我的实际文件中而图片没有存储的问题。

代码如下:

  <?php
           include "config.php";

            if (isset($_FILES['file'])) {
                $name = $_FILES['file']['name'];
                $size = $_FILES['file']['size'];
                $type = $_FILES['file']['type'];
                $tmp = $_FILES['file']['tmp_name'];
                $file = 'uploads'. $_FILES['file']['name'];
                $upload = move_uploaded_file($tmp, $file);
                if ($upload) {
                    $add = $db->prepare("insert into upload values('',?)");
                    $add->bindParam(1, $name);
                    if ($add->execute()) {
                        ?>
                        <div class="alert alert-success alert-dismissible" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <strong>Success!</strong> File upload successful to database.
                        </div>
                        <?php
                    } else {
                        ?>
                    <div class="alert alert-danger alert-dismissible" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <strong>Failed!</strong> File upload unsuccessful to database.
                    </div>
                    <?php
                    }
                } else {
                    ?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <strong>Sorry!</strong> File upload unsuccessful .
                    </div>
                    <?php
                }
            }
            ?>

这是我的档案: enter image description here

在我回复$ file&amp; $ TMP: enter image description here

2 个答案:

答案 0 :(得分:1)

更改此

$file = '../uploads/'. $_FILES['file']['name'];

到这个

   hdc = CreateDC("WINSPOOL", printerName, "", pdevMode);

您的实际代码会将上传文件夹搜索到您的代码所在的uploadTesting文件夹中。

答案 1 :(得分:0)

您需要从代码中提供上传目录的相对路径。 对于前者如果上传目录是文件所在的文件夹之外的一个文件夹。

'../uploads/'.$fileName

另外,请确保您拥有该目录的写入权限。