I am completely new to PHP but have had to learn what I can really quick for something at work. I am trying to create a form which then saves the answers to a row in a database and the images on the server. The below code is from tutorials etc. across the net with my additions in there. I think I'm 90% there apart from saving the image URLs in the database. I'm guessing it's something wrong in the "foreach" section in the code.
What I would like to achieve is to save each file upload URL in a column in the database. At present I've only created the imageurl field but I'm guessing I would need imageurl2 etc. I've tried searching for the next part but cannot find it. I'm assuming what I really need to achieve is a way for the code to call up all of the multiple file upload URLs (array) and then do the SQL insert after?
HTML code:
<form action="form.php" method="post" enctype="multipart/form-data">
<label>Date:</label>
<br>
<input name="date" type="date">
<label>Name:</label>
<br>
<select name="visitor">
<option value="name1">Name One</option>
<option value="name2">Name Two</option>
</select>
<label>Pub:</label>
<br>
<select name="pub">
<option value="pubOne">Pub One</option>
<option value="pubTwo".Pub Two</option>
</select>
<label>Comment:</label>
<br>
<textarea cols="30" rows="5" name="comment"></textarea>
<label>Upload Image:</label>
<br>
<input name="fileToUpload[]" type="file" multiple="">
<input class="formbtn" type="submit" value="Submit">
</form>
PHP Code - I've replaced the database connection details with "xxx" however I'm connecting fine on my code
<?php
print_r($_POST);
$servername = 'xxx';
$username = 'xxx';
$password = 'xxx';
$dbname = 'xxx';
// Create connection to server
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "<div><center><h1>Connected successfully</h1></center></div>";
// Get all the values from input
$date = $_POST['date'];
$visitor = $_POST["visitor"];
$pub = $_POST['pub'];
$comment = $_POST['comment'];
date_default_timezone_set('UTC');
if (!file_exists("uploads/" .date('Y') . '/' .date('m') . '/')) {
mkdir("uploads/" .date('Y') . '/' .date('m') . '/', 0777, true);
}
require_once('ImageManipulator.php');
foreach($_FILES['fileToUpload']['tmp_name'] as $key => $tmp_name) {
if ($_FILES['fileToUpload']['error'][$key] > 0) {
// Insert data into table
$sql = "INSERT INTO visits (date, visitor, pub, comment)
VALUES ('$date', '$visitor', '$pub', '$comment')";
} else {
$image_mime = image_type_to_mime_type(exif_imagetype($_FILES['fileToUpload']['tmp_name'][$key]));
$allowedFiles = array("image/png","image/jpeg","image/gif");
echo "File type:" . $image_mime . "<br>";
if(!in_array($image_mime, $allowedFiles)){
echo "The picture is not allowed.<br>";
} else
{
$newNamePrefix = time() . '_';
$manipulator = new ImageManipulator($_FILES['fileToUpload']['tmp_name'][$key]);
// resizing to 200x200
$newImage = $manipulator->resample(200, 200);
// saving file to uploads folder
$target_dir = "uploads/" .date('Y') . '/' .date('m') . '/';
$target_file = $target_dir .date('Ymd-his') . '-' . $visitor . '-' . basename($_FILES["fileToUpload"]["name"][$key]);
$manipulator->save($target_file);
// Insert data into table
$sql = "INSERT INTO visits (date, visitor, pub, comment, imageurl) VALUES ('$date', '$visitor', '$pub', '$comment', '$target_file')";
echo 'Image resized and uploaded!<br>';
}
}
}
if ($conn->query($sql) === TRUE) {
echo "<p>New record created successfully</p>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
</div>