I am trying to insert the paths to one user uploaded image($picpath), one user input($uname) and to an html file with variable content($profilepath) into a MySQL table like so:
<?php
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
$temp = explode(".", $_FILES["fileToUpload"]["name"]);
$imageFileType = end($temp);
$random = uniqid();
$imgname = "img$random.$imageFileType";
$picpath = "uploads/$imgname";
if ($uploadOk == 0) {
echo "sorry not able to upload file";
}
else {
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "uploads/ . $imgname)) {
echo "the file " .$imgname. " has been uploaded!";
}
else {
echo "sorry there was an error.";
}
}
$uname = $_POST['uname'];
$filecount = count(glob("usertest/*.html"));
$filename = "user" .($filecount+1).".html";
$profilepath = "usertest/$filename";
$myfile = fopen("usertest/$filename", "w") or die("unable to create file");
$html = "<html><body><h1>this is" .$uname."'s profile</h1></body></html>";
fwrite($myfile, $html);
fclose($myfile);
$servername = "127.0.0.1";
$username = "username";
$password = "Password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO users(id, name, profilepath, picpath) VALUES (NULL, '$uname','$picpath', '$profilepath')";
if ($conn->query($sql) === TRUE) {
echo "New record created!";
}
else {
echo "Error.";
}
$conn->close();
?>
From the following HTML/JS:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script>
$('.button').click(function(){
var unameValue = $('input:text').val();
var actionValue = $(this).val();
var ajaxurl = 'upload.php',
data = {
'uname': unameValue,
};
$.post(ajaxurl, data, function(response) {
alert("success!!!!");
});
});
</script>
</head
<body>
<form method="POST">
<input id="userinput" type="text" name="uname" value="peter" />
</form>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" class="button" value="submit" />
</form>
</body>
</body>
</html>
However when I run the code I see that two html files have been created(when only wanting one). What is more, they are named differently (e.g. user1.html, user2.html). Also when I check the MySQL database the following appears:
ID Name picpath profilepath
1 Peter usertest/user1.html uploads/img19ckdovj239si.
2 usertest/user2.html uploads/img19ckdovj239si.png
3 George usertest/user3.html uploads/img19ckdovuv34yu.
usertest/user4.html uploads/img19ckdovuv34yu.png
And so on(the random image numbers are made up). It is really weird! I should also add that sometimes the two images would have the same uniqid()(as seen above) and sometimes they wouldn't.
I tried everything I could find online, but it seems like nobody has encountered this problem!
Any help would be greatly appreciated!
Thanks!
EDIT:
This is what the html should look like (it's much simpler, and without js!):
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload"/>
<input id="userinput" type="text" name="uname" value="peter" />
<input type="submit" class="button" value="submit" />
</form>
</body>
</body>
</html>
It uses the same PHP.