I have to select a pdf file in my step1 page then go to step2 page fill in other details. Once the form is submitted, all the data should be stored into database.
The error showed: Undefined index pdf_file
Here is my coding ..
<form method="post" enctype="multipart/form-data">
<div class="form-group">
<label><i class="zmdi zmdi-upload"></i></label>
<input type="file" name="pdf_file" id="pdf_file" accept="application/pdf" />
</div>
Step2.php
$con = mysqli_connect("localhost","root", "","db");
$pdf = $_POST['pdf'];
if(isset($_POST['confirm'])){
//upload file
$allow=array('pdf');
$temp=explode(".", $_FILES[$_POST['pdf_file']]['name']);
$extension=end($temp);
$uploadfile=$_FILES[$_POST['pdf_file']]['name'];
$appID="A0003";
$sql=mysqli_query($con, "INSERT INTO application_table (appID, pdf) VALUES ('$appID','$uploadfile')");
if($sql){
echo "okay";
}else{
echo "failed";
}
}
What mistakes I made?
Related
I am trying to upload an image file to my server using some code I found on the internet but it doesn't work and I can't find out why.
<form method="post" action="?p=edit-profil&id=<?php echo($user_id); ?>">
<h2>Profil bearbeiten</h2>
<hr />
<h4>Profilbild ändern</h4>
<p style="margin-top: 5px;">(Zulässige Dateiformate: .png .jpg)</p>
<input type="hidden" name="size" value="1000000" style="padding: 6px;">
<div>
<input type="file" name="image" class="choose-image">
</div>
<div>
<input type="submit" name="add-personal-data" value="Speichern" class="submitbutton">
</div>
</form>
// Get image name
$image = $_FILES['image']['name'];
// image file directory
$target = "img/".basename($image);
$image_insert = 'img/'.$image;
$image_upload_statement = $pdo->prepare("UPDATE beschreibung SET profilbild = '$image_insert' WHERE user_id = '$user_id'");
$image_upload_statement->execute();
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
So usually image_insert should be someting like img/picture.png but I only get img/ and there is no upload aswell. Since I am quit new with working with this topic I have no more idea how to fix the problem.
Your form tag should have this attribute enctype="multipart/form-data" to be able to send the image to the php file.
Im attempting to allows users to choose from a simple set of 4 avatars that they can use as their profile picture.
I've tried using:
<input type="image" class="profile-image-icon" src="img/avatar/avatar1.png" name="image" id="image">
But I finally found that using input type="image" is only for using an image as a submit button.
The avatars in question are already in my img/avatar folder, which I then only want to store the image path in the database.
I have echoed any error messages on changeProfile.php, which is returning "Nothing getting through" so I know it is an issue with my form, but I am unsure how to proceed.
Is there anyway to allow the user to simply click on the image and it 'POSTS' to changeProfile.php where I can then retrieve the file path to store in my database?
Thanks for any help provided!
edit_profile_image.php
<form class="profile-image-form" method="POST" action="profileChanged.php">
<fieldset>
<div class="image-control">
<input type="image" class="profile-image-icon"src="img/avatar/avatar1.png" name="image" id="image">
</div>
<?php echo "<input type='text' class='form-control' id='studentNumber' name='studentNumber' value='$studentID'>"; ?>
</fieldset>
</form>
changeProfile.php
if (isset($_POST['image'])) {
$image = mysqli_real_escape_string($conn, $_POST['image']);
$studentNumber = mysqli_real_escape_string($conn, $_POST['studentNumber']);
} else {
echo "Nothing coming thorugh";
}
I have an html form that is used to upload text files:
<div class="form">
<h3>Upload File:</h3>
<form action="networkSelector.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">
<label for="UploadFileField"></label>
<input type="file" name="UploadFileField" id="UploadFileField" />
<input type="submit" name="UploadButton" id="UploadButton" value="Upload" />
</form>
</div>
The php portion of the code for the form:
<?php
require('db.php');
include("auth.php");
if(isset($_FILES['UploadFileField'])){
// Creates the Variables needed to upload the file
$UploadName = $_FILES['UploadFileField']['name'];
$UploadName = mt_rand(100000, 999999).$UploadName;
$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
$UploadType = $_FILES['UploadFileField']['type'];
$FileSize = $_FILES['UploadFileField']['size'];
// Removes Unwanted Spaces and characters from the files names of the files being uploaded
$UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);
// Upload File Size Limit
if(($FileSize > 125000)){
die("Error - File too Big");
}
// Checks a File has been Selected and Uploads them into a Directory on your Server
if(!$UploadTmp){
die("No File Selected, Please Upload Again");
}else{
move_uploaded_file($UploadTmp, "C:/xampp/htdocs/meg/$UploadName");
}
}
?>
It works great and as seen in the 'move_upload_file command it puts them directly into that directory.
However what I am trying to achieve is to upload these files with this form and then to add it to another form that is on the same page.
Here is an example of my other form:
<form action="networkCompiler.php" method="POST">
<h3>Choose Network/Function:</h3>
<select id ="network" name="network" />
<option value="networkA">A</option>
<option value="networkB">B</option>
</select>
So ideally if I upload networkC on the first form, I want it to then display on the second form. I am using PHP primarily on this project and was attempting to find a solution in that language. So far I have tried saving the file upload as a variable and then adding that to the bottom of the form.
<?php
if (isset($_POST['UploadButton'])) {
if (is_uploaded_file($_FILES['UploadFileField']['tmp_name'])) {
$trying = $_POST['FileUploadForm'];
}
}
?>
Any input would be appreciated. Thank you.
Use file_get_contents():
<?php
if(your_condition)
echo '<option value="the_value_you_want">' . file_get_contents('path_of_uploaded_file') . '</option>';
?>
I have a mobile web app form where I access the mobile device camera and capture a photo along with some other details captured within other fields in the form. Using php I save the information captured in the form to a mysql database sucessfully, but the image is not stored. I have searched for a resolution but cannot find anything on using php with
<input type="file" accept="image/*;capture=camera">
to store the image in a database. The database field is currently blob but this can change if needed.
The form works perfectly for all other data, so the issue is with my lack of understanding of how to handle images or files with php. can anyone help or point me in the right direction please. The basis of my code is pasted below.
The form save is working fine as the bus_name input saves to the database, but the bus_img record is blank.
HTML
<div data-role="page" id="view_record">
<div data-role="header">
Back
<div data-role="main" class="ui-content">
<form method="post" enctype="multipart/form-data" action="saveRecord.php">
<label for="bus_name">Business Name:</label>
<input type="text" name="bus_name" id="bus_name" placeholder="Enter Business Name">
<label for="bus_type">Business Type:</label>
<input type="text" name="bus_type" id="bus_type" placeholder="Enter Business Type">
<label for="bus_tel">Business Tel:</label>
<input type="text" name="bus_tel" id="bus_tel" placeholder="Enter Business Tel No">
<label for="bus_img">Business Photo:</label>
<input type="file" name="bus_img" id="bus_img" accept="image/*;capture=camera">
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" placeholder="Enter Comments"></textarea>
<input type="submit" value="Submit">
</form>
</div>
</div>
</div>
PHP
<?php
$bus_img = $bus_name = "";
$servername = "";
$username = "";
$password = "";
$dbname = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());}
$bus_img = $_FILES["bus_img"];
$bus_img = mysqli_real_escape_string($conn, $bus_img);
$bus_name = $_POST["bus_name"];
$sql = "INSERT INTO tblLead(leadImage, occupantName)
VALUES ('$bus_img','$bus_name')";
if (mysqli_query($conn, $sql))
{
} else
{
echo "Error: " . $sql . mysqli_error($conn);
}
mysqli_close($conn);
exit();
}
?>
ANy help much appreciated. Thanks
Firstly, file handling requires $_FILES and not $_POST.
Your form tag does not contain a proper enctype to handle files.
As per the manual's example on files handling:
<form enctype="multipart/form-data" action="__URL__" method="POST">
Reference:
http://php.net/manual/en/features.file-upload.post-method.php
Then you need to escape that (file) data and there are a few ways to do this.
One of which being mysqli_real_escape_string($conn, $file)
Reference:
http://php.net/manual/en/mysqli.real-escape-string.php
Something you should also be using against all your data as it is presently open to an SQL injection.
I.e. and by replacing:
$bus_img = $_POST["bus_img"];
with:
$bus_img = $_FILES["bus_img"];
$bus_img = mysqli_real_escape_string($conn, $bus_img);
And make sure that the file does not exceed the maximum uploaded size allowed/set on your server.
Use proper error checking also.
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.error-reporting.php
I am trying to store an image into an mysql database but in the ajax page $_FILE['textfield_Name']['temp_name'] is not recognizing it. Help me to solve this problem.
This is my file page code
<form class="form-horizontal signUPForm" method="post" action="most_wanted_ajax.php">
<label for="images">Upload Image</label>
<input type="file" name="images">
<br>
<button type="submit" id="add" value="Submit" >Add Most Wanted</button>
</form>
my jquery code is:
$("#add").click(function (e){
$.post($(".signUPForm").attr("action"),$(".signUPForm").serializeArray(),function(res){
if(res!=null){
alert("mosted wanted added");
}
else
{
alert("Somthing is fishy");
}
});
});
and here is my most_wanted_ajax.php code
<?php
if(getimagesize($_FILES['images']['tmp_name'])==FALSE)
{}
else
{
$image=addslashes($_FILES['images']['tmp_name']);
$image= file_get_contents($image);
$image= base64_encode($image);
$con=mysqli_connect("localhost","root","","ecopsweb");
$insertSQL="insert into most_wanted(images) values('".$image."')";
$rs= mysqli_query($con, $insertSQL);
$id= mysqli_insert_id($con);
header("location: missing_person_info.php");
}
?>
in the most_wanted_ajax.php page give two errors
Notice: Undefined index: images in
C:\wamp\www\eCopsWeb\adminModule\most_wanted_ajax.php on line 3
Warning: getimagesize(): Filename cannot be empty in
C:\wamp\www\eCopsWeb\adminModule\most_wanted_ajax.php on line 3
Add enctype="multipart/form-data"> to your form tag.This value is required when you are using forms that have a file upload control
It would be
<form class="form-horizontal signUPForm" method="post" action="most_wanted_ajax.php" `enctype="multipart/form-data"`>