Submitting a bootstrap form not working - php

I'm trying to submit my Bootstrap form using php so that the data entered into the form is emailed to me from the page. Something somewhere is not working though, and unfortunately my php knowledge is lacking (and web searches have so far brought up nothing of use).
When you click submit the modal which the form is in instantly closes (a default HTML thing?). Re-opening the modal displays the the form error messages if you have left forms blank. If you submit the form with all the content present, nothing happens.
So my question is this: What have I done wrong, and how do I fix it to get the email to send? Also, can I stop the modal from closing when content is submitted but not complete?
EDIT: I have implemented the "correct" php for uploading an image, but it gives me an invalid file type even when they are valid. echo $target_file only returns the file_dir suggesting it is failing to ge the name of the file. What have I done wrong?
Here is the php:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$location = $_POST['location'];
$desc = $_POST['desc'];
//Image upload
$target_dir = "/img/gallery/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["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_file)) {
echo "Sorry, file already exists. Please rename your image.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large. Google how to reduce an image size or contact us if you need help with this.";
$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["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
$from = 'Gallery Upload Form';
$to = 'email#example.com'; //I've only changed this for stackoverflow
$subject = 'New Gallery Image';
$body = "Name: $name\n Location: $location\n Description: $desc\n \n $file";
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['location']) {
$errLocation = 'Please enter where your image was taken';
}
if (!$_POST['desc']) {
$errDesc = 'Please enter a description of your image';
}
if (!$_POST['file']) {
$errFile = 'Please upload your image file';
}
// If there are no errors, send the email
if (!$errName && !$errName && !$errLocation && !$errDesc && $errFile) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! Your image has been submitted.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later or report this to us.</div>';
}
}
}
?>
And here is the HTML:
<div class="box">
<h2>Upload your picture</h2>
<p>Upload your picture and we'll upload it to the gallery. The best picture each month will be featured as the page background. You can also upload your pictures to our Facebook page with
<font color="darkcyan">#gallery</font>.</p>
<br>
<!-- model content -->
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModalNorm">Upload a Picture</button>
<!-- Modal -->
<div class="modal fade" id="myModalNorm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h3 class="modal-title" id="myModalLabel">Upload Image</h3>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form role="form" method="post" action="index.php">
<div class="form-group">
<label for="name">Your Name</label>
<input class="form-control" placeholder="John Smith" type="text" name="name" required value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label for="location">Location</label>
<input class="form-control" placeholder="South-west bank of Trout Pool." type="text" name="location" value="<?php echo htmlspecialchars($_POST['location']); ?>">
<?php echo "<p class='text-danger'>$errLocation</p>";?>
</div>
<div class="form-group">
<label for="desc">Picture Description</label>
<textarea class="form-control" name="desc" rows="3" placeholder="A picture of a 6lbs trout, my biggest all season."><?php echo htmlspecialchars($_POST['desc']); ?></textarea>
<?php echo "<p class='text-danger'>$errDesc</p>";?>
</div>
<div class="form-group">
<label for="btn-upload">Upload Image File</label>
<input type="file" id="exampleInputFile" name="file" value="<?php echo htmlspecialchars($_POST['file']); ?>">
<?php echo "<p class='text-danger'>$errFile</p>";?>
</div>
<input id="submit" name="submit" type="submit" value="Upload" class="btn btn-primary">
</form>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php echo $result; ?>
</div>
EDIT: A stupid question - Do you have to configure anything server-side for the email to be sent?

Your uploading a file your html form must have the enctype="multipart/form-data"
attribute.Use $_FILES variable to access information regarding uploaded file.
check these link for more information on $_FILES
http://php.net/manual/en/reserved.variables.files.php
http://www.tutorialsscripts.com/php-tutorials/php-files-variable.php

Related

Form with Upload file attachment

I am using contact.php for contact form. I added an attachment field for upload file/image, pdf, docx in contact form using . But i can not integrate php functions for file attachment. I am weak in php, but this is very important for me to add this in my contact form.
Please someone help me. My form code and php code are given below. Thanks in advance.
//** This is html markup
<form id="Frmgroupa" class="form-style" method="post" onsubmit="return validator4(this)" action="contact.php">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Name of Candidate *</label>
<input type="text" id="name" class="form-control" name="name" placeholder="Name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" placeholder="" required autofocus />
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_need">State</label>
<select id="State" class="form-control" name="State" placeholder="" required autofocus>
<option value="UTTAR PRADESH">UTTAR PRADESH</option>
</select>
</div>
</div>
<div class="col-md-6">
<p>Address Proof *</p>
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="Address_Proof" name="filename" >
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
<div class="col-md-6">
<p>Identity Proof *</p>
<div class="custom-file mb-3">
<input type="file" class="custom-file-input" id="Identity_Proof" name="filename">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
</div>
</div>
<input type="submit" class="btn btn-warning btn-3d" value="SUBMIT" id="submit-button" />
<?php unset($_SESSION['cf_returndata']); ?>
</form>
//* This is the contact.php code
<?php
$myemail = "amit.joshi98#gmail.com";
$subject = "file Upload";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$State = $_POST['State'];
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = 'http://google.com';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $name
State: $State
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thank-you.html');
exit();
/* Functions we used */
function check_input($data, $problem = '')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
First you need to write the following phrase in the form tag
enctype="multipart/form-data"
Like this ...
<form action="process.php" method="post" enctype="multipart/form-data">
and then in process.php file check submit and save file in your path
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
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;
}
}
for limit file Type
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
for last check error and set message for user
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
you can Check if file already exists or Check file size
But i suggest you write an uploader function for the file and use it wherever you want.
In this example, photo file types were used that you can change according to your needs

Is it possible to have multiple enctypes on an html form?

I'm very new to any web development issues and I couldn't find a solution to this problem. I have a form for uploading files so I use the enctype="multipart/form-data". However, I was also trying to take the fields from the form and encode them in a JSON file and read those out in a table in my html on my webpage. With the default enctype, the JSON encoding works great but obviously the file upload functionality doesn't work and vice versa. I followed a lot of W3Schools tutorials but I'm still lost. Is it possible to use two enctypes or what is the work around to this problem? I'm only uploading my files to localhost, no databases used. I apologize if I give too much info here but my form html looks like:
<form class="modal-content animate" enctype="multipart/form-data" method="post">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<div class="container">
<label for="artist"><b>Artist Name</b></label>
<input class="form-control" type="text" placeholder="Enter name of artist" name="artist" required>
<label for="song"><b>Song Name</b></label>
<input class="form-control" type="text" placeholder="Enter name of song." name="song" required>
<label for="instrument"><b>Instrument Type</b></label>
<input class="form-control" type="text" placeholder="Enter type of instrument for tab." name="instrument" required>
<label for="myfile"><b>Tablature</b></label>
<input class="form-control" type="file" placeholder="Select your file." name="myfile" id="myfile" required>
<input type="submit" name="submit" value="Upload Tab PDF" class="w3-hover-purple" style="color:black; background-color:#A9A9A9"></input>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</form>
My php code is structured as such:
<?php
$message = '';
$error = '';
if(isset($_POST["submit"])) {
if(empty($_POST["artist"])) {
$error = "<label class='text-danger'>Enter artist</label>";
}
else if(empty($_POST["song"])) {
$error = "<label class='text-danger'>Enter song name</label>";
}
else if(empty($_POST["instrument"])) {
$error = "<label class='text-danger'>Enter instrument</label>";
}
else if(empty($_POST["myfile"])) {
$error = "<label class='text-danger'>Enter file to upload</label>";
}
else {
if(file_exists('tablature.json')) {
$current_data = file_get_contents('tablature.json');
$array_data = json_decode($current_data, true);
$extra = array(
'artist' => $_POST['artist'],
'song' => $_POST['song'],
'instrument' => $_POST['instrument'],
'myfile' => $_POST['myfile']
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('tablature.json', $final_data)) {
$message = "<label class='text-success'>File Appended Successfully.</label>";
}
else {
$error = 'JSON File does not exist';
}
}
}
$target_dir = "Tablature/";
$target_file = $target_dir . basename($_FILES["myfile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file already exists
if (file_exists($target_file)) {
$error = "Sorry, file already exists.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$error = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["myfile"]["tmp_name"], $target_file)) {
$message = "The file has been uploaded.";
} else {
$error = "Sorry, there was an error uploading your file.";
}
}
}
?>
Finally, on my page I print out my JSON contents on my page like so:
<table style="width:100%">
<tr>
<th>Artist</th>
<td>Song Name</td>
<td>Instrument Type</td>
<td style="float:right; margin-right:100px;">File</td>
</tr>
<?php
$read_data = file_get_contents("tablature.json");
$read_data = json_decode($read_data, true);
print("Working<br>");
foreach($read_data as $row) {
print('<tr><th>'.$row["artist"].'</th><td>'.$row["song"].'</td><td>'.$row["instrument"].'</td><td style="float:right">'.$row["myfile"].'</td></tr>');
}
?>
</table><br>
Any help would be greatly appreciated. Thank you in advance.

PHP does not upload image file to folder

I made a function to upload an image to my database and upload the image to a folder.
The url in the database changes to the url of the image but the file does not upload to the folder.
I get the error: Undefined index: user_image in image.php
Here is my code:
Image.php
<?php
$edit_row['opzoekImage'] = $_POST["user_image"];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
if($imgFile)
{
$upload_dir = 'user_images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions))
{
if($imgSize < 5000000)
{
unlink($upload_dir.$edit_row['opzoekImage']);
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else
{
$errMSG = "Sorry, your file is too large it should be less then 5MB";
}
}
else
{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
else
{
$userpic = $edit_row['opzoekImage']; // old image from database
}
?>
New.php
<?php
if(isset($_POST["submit"]) && isset($_GET['website_naam']) && isset($_GET['tbl_name']) && $_GET['tbl_name'] == "tblOpzoek") {
include('config.php');
$website_naam = $_GET['website_naam'];
$sqlWebsiteId = "SELECT websiteId FROM tblWebsite WHERE websiteNaam = '$website_naam'";
if($db->query($sqlWebsiteId) != "") {
$resultWebsiteId = $db->query($sqlWebsiteId);
if ($resultWebsiteId->num_rows > 0) {
// output data of each row
while($row = $resultWebsiteId->fetch_assoc()) {
$websiteId = $row["websiteId"];
}
include('image.php');
$sqlToevoegenType = "INSERT INTO tblOpzoek (websiteId, opzoekName, opzoekValue, opzoekImage) VALUES ('".$websiteId."', '".$_POST["typeNaamToevoegen"]."', '".$_POST["typeWaardeToevoegen"]."', '".$userpic."')";
if($db->query($sqlToevoegenType) === TRUE) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sqlToevoegenType . "<br>" . $db->error."');</script>";
}
} else {
echo "0 results";
}
}
}
?>
My form:
Good to notice I do have more forms with file input name="user_image"
<!-- Type toevoegen-->
<div class="modal fade" id="addType" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="php/new.php?website_naam=<?php echo $websiteNaam ?>&tbl_name=tblOpzoek" method="post" id="formTypeToevoegen">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Type toevoegen</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>Type Naam</label>
<input type="text" id="typeNaamToevoegen" name="typeNaamToevoegen" class="form-control" >
</div>
<div class="form-group">
<label>Type Waarde</label>
<input type="text" id="typeWaardeToevoegen" name="typeWaardeToevoegen" class="form-control" >
</div>
<div class="form-group">
<label>Type Afbeelding</label>
<input class="input-group" type="file" id="videoUploadFile" name="user_image" accept="image/*" />
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Sluiten</button>
<button type="submit" name="submit" class="btn btn-primary">Gegevens opslaan</button>
</div>
</form>
</div>
</div>
</div>
Thanks for your time!
You need to add the enctype attribute to your <form> tag
<form enctype="multipart/form-data">
As per your error first check what name you define in input tag because if you define different name in input tag and define different name in $_FILES then it shows error.
So define same name in input tag and $_FILES e.g.
$_FILES['user_image']['name'];
If above are correct then check whether you add enctype="multipart/form-data" in form or not.

How to make a form which INCLUDES an upload file input [duplicate]

This question already has an answer here:
PHP file uploads doesnot read $_FILES['image'] [duplicate]
(1 answer)
Closed 7 years ago.
Hello I'm trying to create a form which includes an upload file input.
My form is sending data to my database while the file uploaded is stored in the img folder.
Now the form without the upload input works fine. The upload script without the form works fine too. But I can't manage to make them work together. I tried to include the "upload script" in my main form script but it didn't worked. I actually don't khow how this procedure should be done.
What I would like to do is that when I submit the form the data goes in the database and the upload file in my img folder. I think the problem comes somehow from the submit button.
Sorry for posting all my code but I think it's necessary to understand...
This is my addOutfit.php (which sends the data of my form to my database)
require("ajax/db.php");
$message = " ";
if ( $_POST ) {
if (!empty($_POST['type'])){
$title = htmlspecialchars($_POST['title']);
$description = htmlspecialchars($_POST['description']);
$brand = htmlspecialchars($_POST['brand']);
$material = htmlspecialchars($_POST['material']);
$type = htmlspecialchars($_POST['type']);
$color = htmlspecialchars($_POST['color']);
$sql = "INSERT INTO outfit (title, description, brand, material, type, color) VALUES('$title', '$description', '$brand', '$material', '$type', '$color')";
$statement = $pdo->prepare($sql);
$statement->execute(['title' => $title, 'description' => $description, 'brand' => $brand, 'material' => $material, 'type' => $type, 'color' => $color]);
$message = "The item has been added";
} else {
$message = "The type field is compulsory";
}
}
?>
<!--AddOutfit form-->
<div class="container-fluid" id="addOutfitForm" >
<div class="col-xs-12 col-md-10">
<form action ="addOutfit.php" method="post" novalidate>
<?php if ( $message ) { ?>
<h3 style="color: red;"><?=$message?></h3>
<?php } ?>
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" rows="3" name="description"></textarea>
</div>
<div class="form-group">
<label for="type">Type</label>
<input type="text" class="form-control" name="type">
</div>
<div class="form-group">
<label for="brand">Brand</label>
<input type="text" class="form-control" name="brand">
</div>
<div class="form-group">
<label for="color">Color</label>
<input type="text" class="form-control" name="color">
</div>
<div class="form-group">
<label for="material">Material</label>
<input type="text" class="form-control" name="material">
</div>
<div class="form-group" action="upload.php" method="post" enctype="multipart/form-data">
<label for="fileToUpload">Picture</label>
<input type="file" name="fileToUpload" id="fileToUpload">
</div>
<button type="submit" class="btn btn-default" name="submit" value="Upload Image">Send</button>
</form>
</div>
This is my upload.php (my uploading documents script)
<?php
$target_dir = "img/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
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;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
Your approach is confusing a bit. You need to check if you have POST request to store the data inside db and check also if you have a file to upload it
if(isset($_POST)) {
// validate and insert the data into db
// if isset $_FILES["fileToUpload"] update the file
if(isset($_FILES) && isset($_FILES["fileToUpload"]['name'])) {
}
}
Also don't forget to add the enctype="multipart/form-data" attribute to the form
<form action ="addOutfit.php" enctype="multipart/form-data" method="post" novalidate>
I think that you just need to add the enctype="multipart/form-data" attribute to your <form> tag.
See http://www.w3schools.com/tags/att_form_enctype.asp
Submitting files only works when the form data is submitted as multipart.
Instead of
<div class="form-group" action="upload.php" method="post" enctype="multipart/form-data"> <label for="fileToUpload">Picture</label>
Have you tried:
<form action ="addOutfit.php" method="post" enctype="multipart/form-data" novalidate>

Technically possible to change OpenCart upload folder to other server?

It is technically possible in OpenCart 2.0 to change upload folder, where customers can upload files? Basically it is defined in config.php (both in root and admin folder) like this:
define('DIR_UPLOAD', '/home/www/shop/system/upload/');
It is possible simple change that destination (for example to another server)?
Currently i'm using this code (simply modified catalog/.../product/product.tpl):
<?php if ($option['type'] == 'file') { ?>
<!-- Button trigger modal -->
<button type="button" class="btn btn-default btn-block" data-toggle="modal" data-target="#myModal">
<i class="fa fa-upload"></i> <?php echo $button_upload; ?>
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?php echo $button_upload; ?></h4>
</div>
<div class="modal-body embed-responsive">
<iframe height="300px" width="350px" frameborder="0" class="embed-responsive-item" src="captcha/captcha.php"></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
After success captcha, runs this index.php in iframe (ran on other server, which is a network attached storage with fix IP):
<!DOCTYPE html>
<html>
<body>
<form action="index.php?new_upload=true" method="post" enctype="multipart/form-data">
File to upload:
<input type="file" name="fileToUpload" id="fileToUpload"><input type="submit" value="Upload" name="submit">
</form>
<?php
if (isset($_GET['new_upload'])) {
upload();
}
function upload() {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0; break;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 10000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0; break;
}
// Allow certain file formats
if($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg" && $fileType != "pdf" && $fileType != "JPG" && $fileType != "JPEG" && $fileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG, PDF & GIF files are allowed.";
$uploadOk = 0; break;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded."; break;
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
chmod($target_file, 0777); //change permission
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>
</body>
</html>
This working fine, and you can easily upload files to server, but i look for a better solution. This didnt connect the order and the uploaded file, like opencart normally do.
Yes it would work, but you have to keep in mind some things.
The folder should he there.
The folder should have appropriate permissions.
It would work fine.

Categories