dpfwhb7470 2016-05-24 04:05
浏览 61
已采纳

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

Before I start please be aware that I will prepare the statements at a later stage.
Moving on I am attempting to have a file be uploaded and be stored in a folder named the days date.
I tried this code but nothing is uploaded and it is not showing as uploading and when I am adding it to the DB it is saying Notice: Undefined variable: randomname all the other fields are being inserted no problem. Header.php contains all the SQL configuration data.

<?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条回答 默认 最新

  • douxia6163 2016-05-24 04:18
    关注

    In the code provided, $randomname won't exist if the if(isset($_POST["submit"])) { check fails, but it will still try to do the database insert no matter what. That is only checking to see if there was an HTML element named "submit" with an assigned value in form that was POSTed.

    First of all, I would move the code starting at $sql = ... inside the if check. Also, a better if check might be to look for something that's a required field:

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

    or better yet, see if it's an actual HTTP POST:

    if ($_SERVER["REQUEST_METHOD"] === "POST") {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog