I've been digging for an answer for days now in how to upload multiple photos into a database without overloading the system, and decided the best way is to upload multiple photos into a newly created directory (created via php) and store the directory link in the database instead. What I'm working on is a form that basically creates a new unique page. This unique page has a unique set of photos, and hence I need to generate a folder each time a page is generated, and upload the path link to the database! How do I do that???
Here is my HTML:
<form method="post" action="test.php" enctype="multipart/form-data">
<p>File:</p>
<input type="file" name="file[]" id="file" >
<input type="submit" value="Upload">
</form>
and here is my PHP so far (should be on the right track I hope :/):
<?php
//Connect to DB
$conn = mysql_connect ('localhost', 'root', 'root');
if (!$conn){
die("Could Not Connect to MySQL!");
}
if(!mysql_select_db("test")){
die("Could Not Open Database:" . mysql_error());
}
echo "<p>Connected</p>";
//Upload Files
foreach ($_FILES['file']['name'] as $f => $name) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $name);
$extension = end($temp);
if ((($_FILES["file"]["type"][$f] == "image/gif")
|| ($_FILES["file"]["type"][$f] == "image/jpeg")
|| ($_FILES["file"]["type"][$f] == "image/jpg")
|| ($_FILES["file"]["type"][$f] == "image/png"))
&& ($_FILES["file"]["size"][$f] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"][$f] > 0){
echo "Return Code: " . $_FILES["file"]["error"][$f] . "<br>";
} else {
if (file_exists("uploads/" . $name)){
echo "<p>File Already Exists</p>";
} else {
//create new directory folder within /uploads
//move the files you upload into the new folder.
move_uploaded_file($_FILES["file"]["tmp_name"][$f], "upload/" . uniqid() . "_" . $name);
//send the file path to the database.
mysql_query("INSERT INTO test (idtest,testing) VALUES (','{$filepath}'");
}
}
} else {
$error = "Invalid file";
}
}
?>
and for those curious, here is my database collumns:
|| idtest (AI, INT) || testing (varchart(50)) ||
Any help is IMMENSELY appreciated! It's been doing me in! Thank you in advance!