在开始之前,请注意我将在稍后阶段准备声明。
继续,我尝试上传一个文件并将其存储在名为“日期”的文件夹中。
我尝试了这段代码,但没有上传任何内容,并且没有显示为正在上传,当我将其添加到数据库时,它显示 注意: 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>
最佳答案
在提供的代码中,如果 if(isset($_POST["submit"])) {
检查失败,$randomname
将不存在,但它会无论如何仍然尝试进行数据库插入。这只是检查是否存在名为“submit”的 HTML 元素,并在已发布的表单中分配了值。
首先,我会将从 $sql = ...
开始的代码移动到 if 检查内。另外,更好的 if 检查可能是寻找必填字段:
if (isset($_POST['photo_project_id'])) {
或者更好的是,看看它是否是一个实际的 HTTP POST:
if ($_SERVER["REQUEST_METHOD"] === "POST") {
关于php - 文件未上传到服务器和变量被认为是未定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404200/