php文件上传到数据库

时间:2016-07-05 09:38:03

标签: php mysql upload

enter image description here我正在尝试将文件上传到目录并将其值存储在数据库中。我不知道这段代码有什么问题。提前感谢您的帮助 图片显示了回响的变量。

$con=connect();
    $file_name=$_FILES['file']['name'];
    $file_size=$_FILES['file']['size']/1024;
    $display_name=$_POST['display_name'];

    $upload_dir='../uploads/docs/';
    $file_temp=$_FILES['file']['tmp_name'];
    $file_path=$upload_dir.$file_name;
if(move_uploaded_file($file_temp,$file_path))
        {
            if($con)
            {
            $query=mysqli_query($con,"insert into dcument_upload 
            values(null,'$display_name','$file_path','$file_size')");
            $rr=mysqli_num_rows($query);
            if($rr)
                {
                    echo 'Uploaded';
                    echo $rr;
                }else
                {
                    echo "Upload failed";
                }
            }
            else
            {
                die("Cannot Connect");

            }
        }   
        else
        {
            echo "<br>Upload Failed<br>Try Again!";
        }

2 个答案:

答案 0 :(得分:1)

试试这个。您将变量放在单引号下。

<?php
    $con = connect();
    $file_name = $_FILES['file']['name'];
    $file_size = $_FILES['file']['size']/1024;
    $display_name = $_POST['display_name'];

    $upload_dir = '../uploads/docs/';
    $file_temp = $_FILES['file']['tmp_name'];
    $file_path = $upload_dir.$file_name;

    if(move_uploaded_file($file_temp,$file_path)) {
        if($con) {
            $query = mysqli_query($con,"insert into dcument_upload values(null, ".mysqli_real_escape_string($con, $display_name).", ".mysqli_real_escape_string($con, $file_path).", ".mysqli_real_escape_string($con, $file_size)".)");
            $rr = mysqli_num_rows($query);

            if($rr) {
                echo 'Uploaded';
                echo $rr;
            } else {
                echo "Upload failed";
            }
        } else {
            die("Cannot Connect");
        }

    } else {
        echo "<br>Upload Failed<br>Try Again!";
    }

答案 1 :(得分:0)

您需要先获取文件的内容:

on applicationShouldTerminateAfterLastWindowClosed_(sender)
    return true
end applicationShouldTerminateAfterLastWindowClosed_

此外,使用mysqli_real_escape_string来转义特殊字符以帮助防止SQL注入。