繁体   English   中英

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

[英]Photo uploading using php into a Mysql database

我目前正在为我的大学最后一年的项目建立一个网站,该网站需要照片上传功能。 当前,当用户上传照片时,照片存储在远程服务器的文件夹中。 我需要将图像保存到数据库中,因此我想知道是否有人在以下代码中对如何执行此操作以及在何处放置代码以将上载的内容发送到数据库方面有任何建议,我也需要它当每个用户上载图像时,这些图像都会全部显示给所有人查看,而不是像现在这样显示,一次只显示一幅图像,刷新页面后,图像就会消失。 希望一切都有意义,任何帮助将不胜感激。 谢谢。

<?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"); ?>

我强烈建议您反对。 取而代之的是,将照片存储在文件夹中,并从数据库中引用它们的位置(即指向它们在文件系统上位置的字符串)。

但是,如果您倾向于将其存储在数据库中,则需要:

  • 上传后获取文件内容
  • 确保内容中没有与SQL冲突的字符(简单的解决方案是以某种方式对其进行编码;也许是base64?)
  • 执行您的SQL插入

再次-这是一个主意。 不要这样做-将其保存到文件系统中。

另外,以下行:

move_uploaded_file($tmp_name, $n);

无需对文件类型或文件完整性进行任何检查,就可以轻松地将Shell上传到您的盒子中。

成功上传文件后,一旦获得上传的图像路径。

    $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);

将图像保存在数据库中。 使用所需的任何字段写插入查询$ file_name,$ file_type,$ file_size和内容。 我假设您能够成功连接到数据库。

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