I have a form in which I collect user informations. Its pretty big and it should have two inputs for image uploading. So it should have two images uploading inside one form. I have a datebase where I store the image URL, and the first image input works perfectly, but I have no idea how can I now upload second image and store its url in the datebase.
My form:
<form action="forms/credit.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" class="input-text required-entry" required="">
<input type="file" name="cofile" class="input-text required-entry" required="">
<input type="submit" name="submit" value="Submit">
</form>
My php image handler:
$targetDir = "../images/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif','pdf');
if(in_array($fileType, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Attempt insert query execution
$sql = "INSERT INTO credit (file, cofile) VALUES ('$fileName', '$fileName')";
if(mysqli_query($link, $sql) == false){
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
}
}
}
So as I said, it works fine for the first image but it doesn't work fine for second. I guess I would need to use array or something like that, but can someone give me a hint or a code preview on how can I do it? I'm fairly new to PHP and I'm still learning a lot.
Thanks!
You need to store the file the same way you store $_FILES["file"]
. This can be accessed using $_FILES["cofile"]
Something along the lines of this should help (untested)
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$cofileName = basename($_FILES["cofile"]["name"]);
$targetcoFilePath = $targetDir . $cofileName;
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath) && move_uploaded_file($_FILES["cofile"]["tmp_name"], $targetcoFilePath))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.