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>