PHP的MySQL图像上传不起作用 [英] PHP tot MySQL image uploading not working

查看:89
本文介绍了PHP的MySQL图像上传不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个网站,我可以将文件上传到我的sql数据库,但它似乎无法正常工作。

I'm trying to make a site in which I can upload a file to my sql database, but it does not seem to work.

这是我的代码;

<html>
    <head>
        <title>Upload an image</title>
    </head>
    <body>
                <form action="upload.php" method="POST" enctype="multipart/form-data">
            File:
            <input type="file" name="Image">
            <input type="submit" value="Upload">
        </form>
        <?php
                    //Connecting to the database
                    mysql_connect("localhost","root","") or die(mysql_error());
                    mysql_select_db("picturedatabase") or die(mysql_error());

                    $file = $_FILES['Image']['tmp_name'];

                    if(!isset($file))
                    {
                        echo "Select an image";
                    }
                    else
                    {
                        $image = addslashes(file_get_contents($_FILES['Image']['tmp_name']));
                        $image_name = addslashes($FILES['Image']['name']);
                        $image_size = getimagesize($FILES['Image']['tmp_name']);
                    }

                    if($image_size==FALSE)
                    {
                        echo "That's not an image.";
                    }
                    else
                    {
                       if(!$insert = mysql_query("INSERT INTO images VALUES('','$image_name','$image')"))
                       {
                           echo "There was a problem uploading the image";
                       }
                       else
                       {
                          $lastid = mysql_insert_id();
                          echo "Image uploaded!<p />Your image:<p /> <img src=show.php?id=$lastid>";
                       } 
                    }
                ?>
    </body>
</html>

当我运行文件时,表单显示(按钮和我也可以选择一个文件),但它也说

And when I run the file, the form stuff shows up (the buttons and I can also select a file), but it also says

"Notice: Undefined index: Image in C:\ProgramFiles\Xampp\htdocs\Database\upload.php on line 16
Notice: Undefined variable: image_size in C:\ProgramFiles\Xampp\htdocs\Database\upload.php on line 29"

有人能告诉我我做错了什么并帮我解决了这个问题吗?

Could someone tell me what I did wrong and help me fix this?

推荐答案

您应该在上传过程中将文件保存在某个文件夹中并将文件名保存在数据库中,以便以后可以从数据库中调用文件名并将其链接为要下载的超链接,我使用以下代码将图像上传到名为 files 的文件夹中,并保存数据库中的文件名。最后我的文件名在变量 $ newname

You should save the files in some folder during the upload process and save the name of file in database, so later you can call the name of file from database and link it as a hyperlink to download, i am using the following code to upload images in a folder called files and saving the name of files in database. At the end i have the file name in variable $newname

    if ($_FILES['file']['name']) {

        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);
        if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/jpg")
                || ($_FILES["file"]["type"] == "image/pjpeg")
                || ($_FILES["file"]["type"] == "image/x-png")
                || ($_FILES["file"]["type"] == "image/png"))
            && ($_FILES["file"]["size"] < 500000)
            && in_array($extension, $allowedExts)
        ) {
            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {
                $ext = end(explode(".", $_FILES["file"]["name"]));
                $filename = current(explode(".", $_FILES["file"]["name"]));
                $newname = $filename . '_' . time() . '.' . $ext;
                move_uploaded_file($_FILES["file"]["tmp_name"],
                    "files/" . $newname);
            }
        } else {
            echo "<div class='alert alert-success'>Image type or size is not valid.</div>";
        }
    }

这篇关于PHP的MySQL图像上传不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆