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"`>
Related
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?
I made theform working with storing the images directly on database and now, I want to learn how to store them in a folder, and store in database just the path.
At this moment I get this error Notice: Undefined index: uploaded_file in and I really don't understand why. Please, some F1 :)
Html form:
<form action="ad_cont.php" method="POST" class="add_contact" name="add_contact" enctype="multipart/form-data">
<input type="file" name="uploaded_file" multiple required>
<input type="submit" value="Upload" class="button">
</form>
Php script:
$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$img_path."')");
You have to add enctype='multipart/form-data' to your form for file uploads to work.
<form action="ad_cont.php" method="POST" class="add_contact" enctype="multipart/form-data" name="add_contact">
<input type="file" name="uploaded_file" multiple required>
<input type="submit" value="Upload" class="button">
</form>
The change php code like this.
$img_path = "images/avatar";
$img_path = $img_path . basename( $_FILES['uploaded_file']['name']);
$img_name= $_FILES['uploaded_file']['tmp_name'];
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $img_path)) {
mysqli_query($conn,"INSERT INTO `uploads` (filename,path)
VALUES ('".$img_name."','".$img_path."')");
Try changing the code of your form like this and hope it will work then.
To upload file to server, you need to mention enctype='multipart/form-data' in the <form>
<form enctype="multipart/form-data" action="ad_cont.php" method="POST" class="add_contact" name="add_contact" >
..
..
</form>
I am trying to submit a form using PHP. I am trying to grab the value from two file inputs in my form, yet when I try to index them with my PHP code, I keep getting an error.
The error I get:
Undefined index: profile-pic in C:\xampp\htdocs\shareitme\form-test.php on line 5
Undefined index: cover-pic in C:\xampp\htdocs\shareitme\form-test.php on line 6
My Code:
<?php
if (isset($_POST['submit'])) {
$profile_pic= time() . $_FILES['profile-pic']['name'];
$cover_pic= time() . $_FILES['cover-pic']['name'];
}
?>
<form id="editprofile" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"/>
<input type="file" name="profile-pic"/>
<input type="file" name="cover-pic"/>
<input type="submit" name="submit" value="submit"/>
</form>
I know I have the names right, what am I doing wrong?
For uploading a file include enctype="multipart/form-data" in your form, like below:
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
The enctype attribute specifies how the form-data should be encoded when submitting it to the server.
For more info on forms : http://www.w3schools.com/tags/att_form_enctype.asp
For renaming a uploaded file, it better to upload it to server with move_uploaded_file and than rename it.
try:
<?php
if (isset($_REQUEST['go']) && $_REQUEST['go'] == "1") {
$profile_pic= time() . $_FILES['profile-pic']['name'];
$cover_pic= time() . $_FILES['cover-pic']['name'];
}
?>
<form id="editprofile" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000"/>
<input type="file" name="profile-pic"/>
<input type="file" name="cover-pic"/>
<input type=hidden name="go" value="1">
<input type="submit" name="submit" value="submit"/>
</form>
Whenever you are want to upload file. you need to add enctype in from.
`enctype="multipart/form-data"'
This should work
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']
?>">
What you're trying to do here, if I'm not mistaken, is upload images on your server, right? You can simply use a third-party upload tool for that. I'd recommend AjaxUpload as it is based on AJAX and pretty easy to implement.
For simplicity, I'm applying the upload functionality on one image. You can simply put it inside a function and reuse it.
<p id="showMsg"></p>
<input type="file" name="profile-pic" id="profilePic" />
JQuery:
$("#profilePic").ajaxUpload({
url : $_SERVER['PHP_SELF'],
name: "file",
onSubmit: function() {
$('#showMsg').html('Uploading ... ');
},
onComplete: function(result) {
$('#showMsg').html('File uploaded with result' + result);
}
});
PHP:
<?php
if (isset($_FILES))
{
$file = $_FILES['file'];
move_uploaded_file($_FILES["file"]["tmp_name"], 'upload_dir/' . $_FILES["file"]["name"]); // This function will upload the image to 'upload_dir' folder. You can modify it as per your requirements.
}
?>
1) if you are using file type then your form attribute should be enctype="multipart/form-data".
<form id="editprofile" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
2) Before using your post value it is best practice to check whether its setted or not.
$pic=isset($_FILES['profile-pic']['name']) ? $_FILES['profile-pic']['name'] : '';
Use enctype="multipart/form-data" in form tag.
Whenever you want to post binary datas you must have to set this enctype attribute.
I made a form at HTML with an option to upload an image. The problem is that when I check the $_FILES array at the php code, it is not found.
The HTML form:
<form method="POST" action="addProduct.php" onsubmit="return checkValidation()">
Image(optional): <input type="file" name="fileImg" accept="image/*" id="fileImg">
<input id="submitProduct" type="submit" name="submitBtn" value="Submit product" onclick="checkValidation()"/>
</form>
The checkValidation() function:
function checkValidation(){
var regex = /\.(jpg|JGP|jpeg|JPEG|png|PNG|gif|GIF)$/;
if (($('#fileImg').val()) && (!(regex.test($('#fileImg').val()))))
{
return false;
}
}
And the php code:
if(isset($_POST["submitBtn"])){
if(!(empty($_FILES)))
{
if (file_exists('uploads/'.$_FILES["fileImg"]["name"]))
{
echo $_FILES["fileImg"]["name"]." already exists. ";
}
else
{
move_uploaded_file($_FILES["fileImg"]["tmp_name"], 'uploads/'. $_FILES["fileImg"]["name"]);
$path = $_FILES["fileImg"]["name"];
}
}
else
$path = 'no_photo.jpg';
The problem is that at the php code, it always jumps to the else branch (which gives me some default image).
Thanks in advance.
Your form lacks the enctype='multipart/form-data' attribute. Without it, $_FILES would always be empty.
Read more about it here: What does enctype='multipart/form-data' mean?
I am trying to get image file name by tag, But when I check it through isset($_FILES['imgFile']), it returns always false.
Here is my HTML tag for getting image file:
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
Here is my php code to retrieve it:
if(isset($_FILES['imgFile']))
{
$img = $_FILES['imgFile']['name'];
echo $img";
}
else
{
echo "Image not set";
}
It always generate "Image not set" as an output though I have selected an image.
Are you using the correct enctype on the form?
<form enctype="multipart/form-data">
This is required when using a file upload element.
just use:
enctype="multipart/form-data"
Say this is your form:
<form action="same_page.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
<input type="submit" name="upload" value="Upload" />
</form>
and your php code to retrive name of the image is in the same page where the form is, then your code should be like this:
<?php
if (isset($_POST["upload"])) {
if (isset($_FILES['imgFile'])) {
$img = $_FILES['imgFile']['name'];
echo $img;
} else {
echo "Image not set";
}
}
?>
But if your php code is in another page, then you only need to use enctype="multipart/form-data" in the form as mentioned in the other answers.