简体   繁体   English

使用php将照片上传到Mysql数据库

[英]Photo uploading using php into a Mysql database

i'm currently making a website for my final year university project, which requires a photo upload function. 我目前正在为我的大学最后一年的项目建立一个网站,该网站需要照片上传功能。 Currently when a user uploads a photo, the photo is stored in a folder in the remote server. 当前,当用户上传照片时,照片存储在远程服务器的文件夹中。 I need the images to go into a database and so I was wondering if anyone had any advice as to how to do this and where to place the code to send the uploaded content to the database within the following code, also I need for it to work where when each individual user uploads an image, they are all displayed for all to see, and not as it is currently, where only one image is displayed at a time and when the page is refreshed, the image disappears. 我需要将图像保存到数据库中,因此我想知道是否有人在以下代码中对如何执行此操作以及在何处放置代码以将上载的内容发送到数据库方面有任何建议,我也需要它当每个用户上载图像时,这些图像都会全部显示给所有人查看,而不是像现在这样显示,一次只显示一幅图像,刷新页面后,图像就会消失。 Hope that all made sense, any help would be greatly appreciated. 希望一切都有意义,任何帮助将不胜感激。 Thank you. 谢谢。

<?php include_once("home_start.php"); ?>
<h1>Upload your images here:</h1>
<div id="fileselect" style="border-bottom:thin #000000 solid; border-   collapse:collapse">
    <form id="frmSimple" action="home.php" method="post" enctype="multipart/form-data">
         Select file to upload:&nbsp;
    <input type="file" id="filename" name="filename" size="10" /><br />
    <input type="submit" id="submit" name="submit" value=" Upload " />                      
    </form>
</div>
<div id="feedback">
        <?php
          // Determine whether a file was uploaded
                 if ($_FILES) {            
                // Put file properties into variables
                $name = $_FILES['filename']['name'];
                $size = $_FILES['filename']['size'];
                $tmp_name = $_FILES['filename']['tmp_name'];                
            // Determine whether file is png, jpg or other
        switch($_FILES['filename']['type']) {
        case 'image/jpeg':  $ext = "jpg";  break;
                case 'image/png':  $ext = "png";  break;
                //default:  ext = '';  break;
            }
            //validate against file type
            //     if $ext is empty string (therefore null or false) image is not a jpg     or png
    if($ext){
                // validate against file size
                if($size < 1000000){
                     // Create a safe name for the file and store in a safe location
                     $n = "$name";  // Could add .$ext to enforce file type
                     $n = ereg_replace("[^A-Za-z0-9.]","",$n);  //  Remove all except   alphanumeric characters and

                     $n = strtolower($n); // Convert to lower case (platform  independence)
                        $n = "uploaded_images/$n"; // Add folder to force safe location
                        move_uploaded_file($tmp_name, $n); // Move to the safe location and give it the safe 

                    echo "<p>Uploaded image '$name' as '$n': </p>";
                     echo "<img src='$n' />";
                }
            else echo "<p>'$name' is too big - 50KB max (50000 bytes).</p>";
            }
            else echo "<p>'$name' is an invalid file - only jpg and png accepted.</p>";
        }
            else echo "<p>No image has been uploaded.</p>";

?>
</div>
<?php include_once("home_end.php"); ?>

I would highly recommend against it. 我强烈建议您反对。 Instead, stored the photos in a folder and reference their location from the database (ie a string pointing to their location on the filesystem). 取而代之的是,将照片存储在文件夹中,并从数据库中引用它们的位置(即指向它们在文件系统上位置的字符串)。

However, if you're so inclined to store it in the database, you need to: 但是,如果您倾向于将其存储在数据库中,则需要:

  • Get the file contents after upload 上传后获取文件内容
  • Ensure that the contents don't have any characters that would conflict with your SQL (easy solution is to encode it somehow; base64 perhaps?) 确保内容中没有与SQL冲突的字符(简单的解决方案是以某种方式对其进行编码;也许是base64?)
  • Perform your SQL insert 执行您的SQL插入

Again - this is a bad idea. 再次-这是一个主意。 Don't do it - save it to the filesystem. 不要这样做-将其保存到文件系统中。

Also, the following line: 另外,以下行:

move_uploaded_file($tmp_name, $n);

without any checks of file type, or file integrity, makes it trivial to upload a shell to your box. 无需对文件类型或文件完整性进行任何检查,就可以轻松地将Shell上传到您的盒子中。

Once after the file uploaded successfully get the uploaded image path. 成功上传文件后,一旦获得上传的图像路径。

    $file_type='jpg';  //if you are using more than one type write a switch case
$file_size = filesize($file);
$fp = fopen($file,'r');
$content = fread($fp,$file_size);
$content = addslashes($content);   //content is a binary data of the image
fclose($fp);

To save the image in database. 将图像保存在数据库中。 Write a insert query with whatever fields you want $file_name, $file_type, $file_size and content. 使用所需的任何字段写插入查询$ file_name,$ file_type,$ file_size和内容。 I am assuming you are able to connect to database successfully. 我假设您能够成功连接到数据库。

mysql_query("INSERT INTO Images (id,file_name,file_type,file_size,content)
   VALUES ('bird', 'jpg',340kb,'binarydata')");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM