文件未上传到数据库。验证好吗?

Posted

技术标签:

【中文标题】文件未上传到数据库。验证好吗?【英文标题】:File not uploading into database. Validation ok? 【发布时间】:2016-02-26 19:23:02 【问题描述】:

我有一个网站,用户可以从手机上传照片,但所有从手机上传的照片在上传时都向左显示 90 度。我解决了这个问题,但现在,在文件经过验证后,它不会进入数据库。请任何帮助..

分享脚本代码为:

<?php

    require('help.php');

    if(isset($_POST['submit']))    
        $title = $_POST['title'];
        $content = $_POST['content'];
        $posted = $_POST['posted'];
        $date = date("Y-m-d H:i:s");
        $ip = $_SERVER["REMOTE_ADDR"];
        $rand = rand(1,1000000);

        if(empty($title))
            echo "Titulli eshte bosh.";
        else if(empty($content))
            echo "Permbajtja eshte bosh.";
        else if(empty($_FILES['file']['name']))
            echo "Imazhi eshte bosh.";
        else if($_FILES['file']['name'])
            $name = htmlspecialchars($_FILES['file']['name']);
            $ext = end((explode(".", $name)));
            $ext = strtolower($ext);

            //if no errors...
            if(!$_FILES['file']['error'])
                //now is the time to modify the future file name and validate the file
                $new_file_name = date('ymdHisu'). ".". $ext;

                if($_FILES['file']['size'] > (6144000))
                  $valid_file = false;
                  echo 'Oops!  Your file\'s size is to large.';
                
                elseif($ext !== "jpg" && $ext !== "png" && $ext !== "jpeg" && $ext != "gif" && $ext !== "bmp") 
                  $valid_file = false;
                  echo "Your file must be in jpg, jpeg, png, gif, or bmp formats.";
                else
                  $valid_file = true;
                

                //if the file has passed the test
                if($valid_file)
                    //move it to where we want it to be
                    move_uploaded_file($_FILES['file']['tmp_name'], 'images/'.$new_file_name);

                    $exif_read = exif_read_data("images/".$new_file_name);

                    if(!empty($exif_read['Orientation']))
                        $orientation_data = exif_read_data("images/".$new_file_name)['Orientation'];
                    

                    if(isset($orientation_data) && $orientation_data !== 1)
                        $path = "images/". $new_file_name;
                        $buffer = ImageCreateFromJPEG($path);
                        $exif = exif_read_data($path);
                        if(!empty($exif['Orientation']))
                            switch($exif['Orientation'])
                                case 8:
                                    $buffer = imagerotate($buffer,90,0);
                                    break;
                                case 3:
                                    $buffer = imagerotate($buffer,180,0);
                                    break;
                                case 6:
                                    $buffer = imagerotate($buffer,-90,0);
                                    break;
                            
                            $image = imagejpeg($buffer, $path, 90);
                        

                    

                    if(empty($posted))
                        $posted = 'Anonim';
                    

                    $sql = "INSERT INTO problems(title, content, date, image, posted, ip) VALUES (:title, :content, :date, :image, :posted, :ip)";
                    $query = $db->prepare($sql);
                    $results = $query->execute(array(
                        ':title' => htmlentities ($title),
                        ':content' => htmlentities ($content),
                        ':date' => $date,
                        ':image' => $path,
                        ':posted' => htmlentities ($posted),
                        ':ip' => $ip
                    ));

                    echo "<div id='ok'>Lajmi u raportua me sukses. Kontrollojeni <a href='index.php'>ketu</a> .</div>";
                
            else
                //set that to be the returned message
                echo 'Ooops!  Your upload triggered the following error:  '.$_FILES['file']['error'];
            
        

    
?>

【问题讨论】:

您想将整个图像上传到您的数据库中...? 不,只有标题、描述和图像名称,所以我从站点中的图像文件夹访问它。 @albtr 看看我的回答。 【参考方案1】:

问题:

文件被验证后,它不会进入数据库。

解决方案

这是因为当您尝试将记录插入数据库时​​,您的 $path 未定义。将$path 变量移到if(isset($orientation_data) &amp;&amp; $orientation_data !== 1) ... 块之外,如下所示:

// your code

$path = "images/". $new_file_name;  // moved this outside of the if block
if(isset($orientation_data) && $orientation_data !== 1)  
    $buffer = ImageCreateFromJPEG($path);
    $exif = exif_read_data($path);
    if(!empty($exif['Orientation']))
        switch($exif['Orientation'])
            case 8:
                $buffer = imagerotate($buffer,90,0);
                break;
            case 3:
                $buffer = imagerotate($buffer,180,0);
                break;
            case 6:
                $buffer = imagerotate($buffer,-90,0);
                break;
        
        $image = imagejpeg($buffer, $path, 90);
    



// your code

【讨论】:

非常感谢。尊重@Rajdeep Paul

以上是关于文件未上传到数据库。验证好吗?的主要内容,如果未能解决你的问题,请参考以下文章

文件已上传到文件夹但路径未存储到数据库中

图像文件成功上传到文件夹,但文件名未保存在数据库中

Laravel 文档多次上传未保存到数据库

电脑使用过程中,内存占用率过高好吗?大概在70%到85%之间!

raw图片格式压缩好吗?

文件未使用 html 输入类型文件上传