未上传到服务器和变量的文件被认为是未定义的

时间:2016-05-24 04:05:09

标签: php mysql sql

在开始之前,请注意我将在稍后阶段准备陈述 继续我试图上传文件并存储在名为日期的文件夹中 我尝试了这段代码,但没有上传,也没有显示为上传,当我将它添加到数据库时,它说Notice: Undefined variable: randomname所有其他字段都插入没有问题。 Header.php包含所有SQL配置数据。

<?php
session_start();
include_once("header.php");
?>
<html>
<body>
<?php 
mysqli_select_db($con,$DATABASE);

    //Checks if the submit button has been pressed.
    if(isset($_POST["submit"])) {
        //Sets the target dir where the image will be moved.
        $target_dir = "/public_html/domain/php/uploads/"  .  date(d/m/Y) . "/";
        //If folder does not exist, create the folder.
        if (!file_exists($target_dir)) {
            mkdir($target_dir, 0755, true);
        }
        //Sets the upload value to 1 ; if the value keeps the value of 1 when passed all the tests, the file will be uploaded.
        $uploadOk = 1;
        //Checks what file extension the image has.
        $imageFileType = pathinfo($_FILES["photo_url"]["name"],PATHINFO_EXTENSION);
        //Creates a random name in MD5
        $randomname = md5(uniqid(true) . $_FILES["photo_url"]["name"]) . "." . $imageFileType;
        // Check if image file is a actual image or fake image
        // Check if image file is a actual image or fake image
        $check = getimagesize($_FILES["photo_url"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
        // Check if file already exists
        if (file_exists($target_dir . $randomname)) {
            echo "Sorry, file already exists.";
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["photo_url"]["size"] > 800000000) {
            echo "Sorry, your file is too large. Max image size is 8mb";
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
        // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES["photo_url"]["tmp_name"], $target_dir . $randomname)) {

                //Echo's that the file has been uploaded.
                echo "The file ". basename( $_FILES["photo_url"]["name"]). " has been uploaded.";

            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        }
    }

$sql="INSERT INTO photo(photo_project_id,photo_section,photo_subsection,photo_date,photo_post,photo_desc,photo_url,photo_dir)VALUES('$_POST[photo_project_id]','$_POST[photo_section]','$_POST[photo_subsection]','$_POST[photo_date]',now(),'$_POST[photo_desc]','{$randomname}','$_POST[photo_dir]')";

if ($con->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}

echo $sql;
if($con = new mysqli($SERVER, $USER, $PASS, $DATABASE)){ echo "success"; }else{ echo "error: " . mysqli_error($con); }

mysqli_close($con);
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在提供的代码中,如果$randomname检查失败,则if(isset($_POST["submit"])) {将不存在,但无论如何,它仍将尝试执行数据库插入。那只是检查是否有一个名为“submit”的HTML元素,其中包含已发布的表单中的指定值。

首先,我会在if检查中移动$sql = ...处的代码。此外,更好的检查可能是寻找必需字段的东西:

if (isset($_POST['photo_project_id'])) {

或者更好,看看它是否是一个真正的HTTP POST:

if ($_SERVER["REQUEST_METHOD"] === "POST") {