I am writing a code for image upload and want to implement AJAX for real time preview. As I have to upload four images on different button and they call different PHP file to upload in respective directory.
My code is working fine for single image upload as it needs to pass form ID.
but not working for all four images.
Please help me out and provide solution.
Below is the code -
<script type="text/javascript">
$(document).ready(function (e) {
$("#upload").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "update_profile_img.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
$("#targetLayer").html(data);
},
error: function()
{
}
});
}));
});
</script>
<div class="col-md-6">
<!-- Horizontal Form -->
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Upload Documents</h3>
</div><!-- /.box-header -->
<!-- form start -->
<?php include('update_profile_img.php');?>
<?php include('update_car_img.php');?>
<?php include('update_licen_img.php');?>
<?php include('update_doc_img.php');?>
<p style="color:green"><?php echo $successMessage;?></p>
<p style="color:red"><?php echo $Error;?></p>
<div class="box-body">
<form role="form" action="#" id="upload" method="POST" enctype="multipart/form-data">
<div class="form-group">
<img class="form-control" style="width:100px; height:100px" alt="" src=<?php echo $target_file; ?> >
<label for="exampleInputFile">Profile Image</label>
<input type="file" class="form-control" name="image" id="image" />
<input type="hidden" class="form-control" name="user_id" value="<?php echo $user_id;?>" />
<button type="submit" name="profile_btn" class="btn btn-primary">Upload</button> <!-- -->
<small>File should be in image format.</small>
</div>
<div class="form-group">
<img class="form-control" style="width:100px; height:100px" alt="" src=<?php echo $target_file1;?> >
<label for="exampleInputFile">Car Image</label>
<input type="file" class="form-control" name="car_image" id="car_image" />
<button type="submit" name="car_btn" class="btn btn-primary">Upload</button>
<small>File should be in image format.</small>
</div>
<div class="form-group">
<img class="form-control" style="width:100px; height:100px" alt="" src=<?php echo $l_img;?> >
<label for="exampleInputFile">Driving License</label>
<input type="file" class="form-control" name="drivelic" id="drivelic"/>
<button type="submit" name="licen_btn" class="btn btn-primary">Upload</button>
<small>File should be in image format.</small>
</div>
<div class="form-group">
<img class="form-control" style="width:100px; height:100px" alt="" src=<?php echo $d_img;?> >
<label for="exampleInputFile">Vehicle Documents</label>
<input type="file" class="form-control" name="vehicledoc" id="vehicledoc"/>
<button type="submit" name="doc_btn" class="btn btn-primary">Upload</button>
<small>File should be in image format.</small>
</div>
</form>
<div class="box-footer">
</div>
</div><!-- /.box-body -->
</form>
</div><!-- /.box -->
</div><!-- /.box-body -->
</div><!--/.col (right) -->
</div> <!-- /.row -->
</section><!-- /.content -->
</div><!-- /.content-wrapper -->
PHP code to upload profile image update_profile_img.php-
<?php
include('../config/conn.php');
$Error ="";
$successMessage ="";
if (isset($_POST['profile_btn'])){
if(!empty($_POST['user_id']) && !empty($_FILES['image']))
{
if($_FILES['image'] != "jpg" && $_FILES['image'] != "png" && $_FILES['image'] != "jpeg" && $_FILES['image'] != "gif" && $_FILES["image"]["size"] > 2097152 )
{
//echo "6";
$Error="Invalid Image!";
}
else
{ $id=$_POST['user_id'];
$target_dir = "images/profile/";
$db_dir = "images/profile/";
$date=rand(0,999);
$datemd=md5($date);
$date=substr($datemd,2,-7);
//$unique_id=substr($date, 3, -3);
$target_file = $target_dir .$date. basename($_FILES["image"]["name"]);
$user_image = $db_dir .$date. basename($_FILES["image"]["name"]);
//$pic=addslashes (file_get_contents($_FILES['image']['tmp_name']));
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file))
{
$sql = "UPDATE pooler SET image='$user_image' WHERE id='$id'";
if ($conn->query($sql) === TRUE)
{
//echo "1";
$successMessage="Profile Image Updated Successfully!";
return '$target_file';
}
else
{
//echo "2";
$Error="Error in Updating Image!";
}
}
else
{
$Error="Image Not Inserted Properly!";
}
}
}
else
{
//echo "4";
$Error="Fill The Required Fields!";
}
}
?>
PHP code to upload car image
<?php
include('../config/conn.php');
$Error ="";
$successMessage ="";
if (isset($_POST['car_btn'])){
if(!empty($_POST['user_id']) && !empty($_FILES['car_image']))
{
if($_FILES['car_image'] != "jpg" && $_FILES['car_image'] != "png" && $_FILES['car_image'] != "jpeg" && $_FILES['car_image'] != "gif" && $_FILES["car_image"]["size"] > 2097152 )
{
//echo "6";
$Error="Invalid Image!";
}
else
{ $id=$_POST['user_id'];
$target_dir = "images/car/";
$db_dir = "images/car/";
$date=rand(0,999);
$datemd=md5($date);
$date=substr($datemd,2,-7);
//$unique_id=substr($date, 3, -3);
$target_file1 = $target_dir .$date. basename($_FILES["car_image"]["name"]);
$user_image = $db_dir .$date. basename($_FILES["car_image"]["name"]);
//$pic=addslashes (file_get_contents($_FILES['image']['tmp_name']));
if (move_uploaded_file($_FILES["car_image"]["tmp_name"], $target_file1))
{
$sql = "UPDATE pooler SET car_image='$user_image' WHERE id='$id'";
if ($conn->query($sql) === TRUE)
{
//echo "1";
$successMessage="Car Image Updated Successfully!";
return 'target_file1';
}
else
{
//echo "2";
$Error="Error in Updating Image!";
}
}
else
{
$Error="Image Not Inserted Properly!";
}
}
}
else
{
//echo "4";
$Error="Fill The Required Fields!";
}
}
?>
I also try to include all upload file code in one file and pass different Target_file variable as target_file and target_file1.
Related
I'm trying to make a new look for my website, but it looks like it doesn't want to login and I don't understand why.. Here's the original page:
<body class="jumbotron vertical-center" onload="redirect();">
<div class="container login-container">
<div class="row">
<div class="col-md-4 login-form mx-auto">
<h3>Login</h3>
<h6>Don't have an account? Register</h6>
<form>
<div class="error-message" id="errorMessage"></div>
<div class="form-group">
<input id="enterID" type="text" class="form-control" placeholder="Your User ID *" value="" required/>
</div>
<div class="form-group">
<input id="password" type="password" class="form-control" placeholder="Your Password *" value="" required/>
</div>
<div class="form-group">
<input class="btn btn-block btn-primary" value="Login" onclick="login();" readonly />
</div>
<div class="form-group">
Trouble logging in?
</div>
</form>
</div>
</div>
</div>
</body>
And here's the edited page:
<!-- To keep it in the center of page-->
<img class="wave" src="img/wave.png">
<div class="container_main">
<div class="img">
<img src="img/bg.svg">
</div>
<div class="login-content">
<form>
<img src="img/avatar.svg">
<h2 class="title">Quiz Website</h2>
<div class="input-div one">
<div class="i">
<i class="fas fa-user"></i>
</div>
<div class="div">
<input type="text" class="input" id="enterID" placeholder="Username">
</div>
</div>
<div class="input-div pass">
<div class="i">
<i class="fas fa-lock"></i>
</div>
<div class="div form-group">
<input id="password" type="password" class="form-control" placeholder="Password" value="" required/>
</div>
</div>
Don't have an account?
<div class="form-group">
Trouble logging in?
</div>
<input type="submit" class="btn" value="Login" onclick="login();" readonly >
<div class="error-message" id="errorMessage"></div>
</form>
</div>
</div>
</html>
What am I doing wrong? I don't want to use the body class jumbotron vertical-center cause it's making my website to look very awful.. I always get ,,XHR loading failed" and I don't understand why..
My checklogin.js file:
function login() {
var enterID = document.getElementById("enterID").value;
var password = document.getElementById("password").value;
if ((password != "") && (enterID != "")) {
var info = "?enterID=" + enterID + "&password=" + password;
$.ajax
({
type: "GET",
url: "php/login.php" + info,
success: function (respond) {
if (respond == "admin") {
window.location.href = "page/admin-system-management.php";
} else if (respond == "student") {
window.location.href = "page/student-dashboard.php";
} else if (respond == "teacher") {
window.location.href = "page/teacher-dashboard.php";
} else {
document.getElementById("errorMessage").innerText = respond;
}
}
});
}
else {
document.getElementById("errorMessage").innerText = "Please fill in all the fields.";
}
}
function redirect() {
$.ajax
({
type: "GET",
url: "php/redirect.php",
success: function (respond) {
if (respond != "not logged.") {
if (respond == "admin") {
window.location.href = "page/admin-system-management.php";
} else if (respond == "student") {
window.location.href = "page/student-dashboard.php";
} else if (respond == "teacher") {
window.location.href = "page/teacher-dashboard.php";
}
}
}
});
}
And my login.php file:
<?php
include "mysql-connect.php";
//get Info from login.html
$ID = $_GET['enterID'];
$PW = $_GET['password'];
$stmt = $connect->prepare("SELECT PW, userType, nickName FROM users WHERE ID = ?");
$stmt->bind_param("s",$ID);
$valid = $stmt->execute();
if (!$valid){
die("Could not successfully run query.". $connect->connect_error);
}
$result = $stmt->get_result();
if ($result->num_rows==0){
//display message of no such student/teacher/admin
echo "Failed to find an account with the input ID.";
} else {
$row = $result->fetch_assoc();
if (password_verify($PW, $row['PW'])) {
$type = $row['userType'];
$nick = $row['nickName'];
//var_dump($row['PW']);
//save data
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_SESSION["type"]=$type;
$_SESSION["userID"]=$ID;
$_SESSION["nickName"]=$nick;
//login success - Request.responseText to checklogin.js
echo $type;
} else {
//display message of password error
echo "The input password does not match the account password.";
}
}
$connect->close();
?>
And here's the redirect.php:
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION["type"])){
if ($_SESSION["type"] != $_SESSION["pageType"]){
$alert_message = "You cannot access this page using your account!";
$link = "../login.html";
echo "<script type='text/javascript'>alert('$alert_message'); window.setTimeout(function(){ if ('".$_SESSION["type"]."'=='admin'){ window.location.href = '../page/admin-system-management.php';} else if ('".$_SESSION["type"]."'=='student'){ window.location.href = '../page/student-dashboard.php';} else if ('".$_SESSION["type"]."'=='teacher'){ window.location.href = '../page/teacher-dashboard.php';} }, 0);</script>";
}
} else {
$alert_message = "Your login period has expired! Please login again!";
$link = "../login.html";
echo "<script type='text/javascript'>alert('$alert_message'); window.setTimeout(function(){ window.location.href = '$link'; }, 0);</script>";
}
?>
I am trying to check if an id selector like (ajax response) has some data in it or not in Bootstrap modal. Based on the result I want to display input field otherwise the input field for data update should not be displayed.
I don't want to display blank input field for data update.
<body>
<!-- The Update Modal -->
<div class="modal" id="update_notes">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Notes Editor</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="form-group"><input type="text" id="up_heading" class="form-control" placeholder="Heading"></div>
<?php
if('.$("#up_notes1").val().' != ""){
>
<div class="form-group"><input type="text" id="up_notes1" class="form-control" placeholder="Notes Details"></div>
<?php
}
?>
<?php
if('.$("#up_notes2").val().' != ""){
?>
<div class="form-group"><input type="text" id="up_notes2" class="form-control" placeholder="Notes Details"></div>
<?php
}
?>
<?php
if('.$("#up_notes3").val().' != ""){
?>
<div class="form-group"><input type="text" id="up_notes3" class="form-control" placeholder="Notes Details"></div>
<?php
}
?>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-success" data-dismiss="modal" onclick="updatenotesdetails()">Save</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<input type="hidden" id="hidden_notes_id">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
load_data();
function load_data(page, query = '')
{
$.ajax({
url:"notes_sa_add_fetch.php",
method:"POST",
data:{page:page, query:query},
success:function(data)
{
$('#dynamic_content').html(data);
}
});
}
//Edit and Update record
function GetnotesDetails(notesid){
$('#hidden_notes_id').val(notesid);
$.post(
"notes_sa_add_bk.php",{
notesid:notesid
},
function(data, status){
var notes = JSON.parse(data);
$('#up_heading').val(notes.HEADING);
$('#hidden_notes_hid').val(notes.HID);
$('#hidden_notes_pid').val(notes.PID);
$('#up_notes1').val(notes.P_1);
$('#up_notes2').val(notes.P_2);
$('#up_notes3').val(notes.P_3);
}
);
$('#update_notes').modal("show");
}
function updatenotesdetails(){
var heading_up = $('#up_heading').val();
var notestype_up = $('#up_notestype').val();
var notes1_up = $('#up_notes1').val();
var notes2_up = $('#up_notes2').val();
var notes3_up = $('#up_notes3').val();
var hidden_notes_id_up = $('#hidden_notes_id').val();
$.post(
"notes_sa_add_bk.php",{
hidden_notes_id_up:hidden_notes_id_up,
heading_up:heading_up,
notestype_up:notestype_up,
notes1_up:notes1_up,
notes2_up:notes2_up,
notes3_up:notes3_up
},
function(data, status){
$('#update_notes').modal("hide");
readRecords();
}
);
}
</script>
</body>
Thanks in advance.
Please change the code to the following to check if up_notes1, up_notes2 and up_notes3 are empty:
<!-- Modal body -->
...
<?php
$up_notes1 = $_POST['up_notes1'];
if(isset($up_notes1) && $up_notes1 != "") {
>
<div class="form-group">
<input type="text" id="up_notes1" class="form-control" placeholder="Notes Details" value="<?php echo $up_notes1; ?>">
</div>
<?php
}
?>
<?php
$up_notes2 = $_POST['up_notes2'];
if(isset($up_notes2) && $up_notes2 != "") {
?>
<div class="form-group">
<input type="text" id="up_notes2" class="form-control" placeholder="Notes Details" value="<?php echo $up_notes2; ?>">
</div>
<?php
}
?>
<?php
$up_notes3 = $_POST['up_notes3'];
if(isset($up_notes3) && $up_notes3 != "") {
?>
<div class="form-group">
<input type="text" id="up_notes3" class="form-control" placeholder="Notes Details" value="<?php echo $up_notes2; ?>">
</div>
<?php
}
?>
...
If you want to check if they are empty before post to server, add the following code to updatenotesdetails():
function updatenotesdetails() {
var heading_up = $('#up_heading').val();
var notestype_up = $('#up_notestype').val();
var notes1_up = $('#up_notes1').val();
var notes2_up = $('#up_notes2').val();
var notes3_up = $('#up_notes3').val();
var hidden_notes_id_up = $('#hidden_notes_id').val();
if(heading_up == '' || notestype_up == '' || notes1_up == '' || notes2_up == '' || notes3_up == '' || hidden_notes_id_up == '') {
alert('Please fill in all fields!');
return false;
} else {
// Post data to server...
}
}
Don't forget to rate if it was helpful.
Thanks.
there is a form I created, but many times emails show up with nothing in the body. The input fields are coming blank. I’ve tried finding the cause, but I haven’t been able to rectify the problem. Does anyone know what might be causing this? and how to resolve this.
I am using this contact form for downloading pdf.
Here is the code:
<div class="modal fade" id="over">
<p id="messageDownload" style="display:none;">Thank you for download, we will contact you shortly!</p>
<div class="modal-dialog">
<div class="modal-content">
<div class="panel panel-default contact bg-edit bg-blue" style="margin-bottom:0;max-width:500px;">
<div class="panel-body">
<form id="loginForm" novalidate="novalidate">
<div class="row" style="max-width:500px;">
<p class="text-uppercase my-font-style" align="center">Fill Your Contact Details</p>
<div class="col-md-12">
<input id="name1" type="text" class="form-control" placeholder="Name" name="name1" required>
</div>
<div class="col-md-12">
<input id="email1" type="email" class="form-control" name="email1" placeholder="Email" required>
</div>
<div class="col-md-12" style="margin-bottom: 15px;">
<input id="mobile1" placeholder="Phone" name="mobile1" type="text" class="form-control input-inverse iti-phone" required>
</div>
<div class="col-md-12 text-center">
<input type="hidden" name="site_name" value="<?php echo $actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; ?>" />
<button onclick="insert()" type="button" class="btn btn-white" name="sub">Download</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<!--End Download Modal-->
<!--start donwload link-->
<div>
<a id="link" href="abc.pdf" class="" download hidden></a>
<button type="button" data-bname="abc.pdf" onclick="document.getElementById('link').href=this.getAttribute('data-bname'); Dat()" class="theme-btn btn-style-one"> Download</button>
</div>
<!--End Download link-->
<!-- start download.js -->
<style>
function insert() {
var name1 = $("#name1").val();
var namev = /^[a-zA-Z ]*$/;
var email1 = $("#email1").val();
var emailv = /^([A-Za-z0-9_\-\.])+\#([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var mobile1 = $("#mobile1").val();
var mobilev = (/^[0-9]+$/);
if (name1 === null || name1 === "" || namev.test(name1) === false) {
alert("Please enter your name.");
return false;
} else if (email1 === null || email1 === "" || emailv.test(email1) === false) {
alert("Please enter email.");
return false;
} else if (mobile1 === null || mobile1 === "" || mobilev.test(mobile1) === false) {
alert("Please enter your mobile.");
return false;
} else {
var formdata = $("#loginForm").serialize() + '&c_code1=' + document.getElementsByClassName('selected-flag')[0].title;
$.ajax({
type: "POST",
url: "download_send.php",
data: formdata,
success: function(data) {
$("#messageDownload").css('display', 'block');
$('#loginForm').html($("#messageDownload"));
$("#messageDownload.p").addClass("alert-default");
},
});
}
document.getElementById('link').click()
setTimeout(function() {
$('#over').modal('hide');
}, 10000);
}
</style>
<!-- end download.js -->
<!-- start download_send.php -->
<?php
date_default_timezone_set("Asia/Dubai");
$ddate = date(' d-m-Y H:i:s');
function get_client_ipsp() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if (isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if (isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if (isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
extract($_POST);
$site_name = str_replace("http://", "", $site_name);
$site_name = str_replace(".php", "", $site_name);
$msg = "Name:- " . $name1 . "\r\n" . "Email:- " . $email1 . "\r\n" . "Phone:- (" . $c_code1 . ")" . $mobile1 . "\r\n" . "IP Addr:- " . get_client_ipsp();
$headers = "From: $name1 <enquiry#abc.com>" . "\r\n" . "CC: enquiry#abc.com" . "\r\n" . "BCC: leads#abc.com";
mail("sales#abc.com", $ddate . ' Enquiry from ' . $site_name . '(Download Form)', $msg, $headers);
echo'your message has been sent';
?>
<!-- end download_send.php --> ```
i've design codes to upload image and data information in hosted server database. The record information save in table. But image not move to server folder. I also try rename() and Copy() but not working.
The strange thing is that i've designed four upload information forms(a,b,c and d) but d is not working. all 3 are working fine. same codes are in all files.
my codes are in files
d.php
<div class="container-fluid">
<!--Form 1-->
<div class="row">
<div class="col-md-6 center" id="shahster">
<div class="panel panel-default shadow">
<div class="panel-heading"><h3 class="panel-title">Shashter Form</h3></div>
<div class="panel-body">
<form class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php $_SERVER["DOCUMENT_ROOT"] ;?>
/dashboard/other/other_sbt.php">
<div class="form-group">
<label class="control-label col-xs-3" for="name">Article Title Name</label>
<div class="col-xs-9"><input type="text" class="form-control" id="name" name="nametxt" placeholder="Enter Title Name" /></div></div>
<div class="form-group">
<label class="control-label col-xs-3" for="desc">Description</label>
<div class="col-xs-9"><textarea rows="3" class="form-control" id="desc" name="desctxt" placeholder="Description"></textarea></div></div>
<div class="form-group">
<label class="control-label col-xs-3" for="upload">Upload:</label>
<div class="col-xs-9"><input id="upload" class="file" type="file" name="upimage" data-min-file-count="1" /></div></div>
<div class="form-group">
<label class="control-label col-xs-3" for="type">Choose options</label>
<div class="col-xs-9">
<label class="checkbox-inline"><input type="checkbox" name="taksalitxt" value="taksali" /> Taksali</label></div></div>
<div class="clearfix"></div>
<div class="form-group">
<label class="control-label col-xs-3">Feature Product</label>
<div class="col-xs-9"><input type="checkbox" name="fproducttxt" value="fproduct"></div></div>
<div class="form-group">
<div class="col-xs-offset-3 col-xs-9">
<input type="submit" class="btn btn-primary" name="submit" value="Submit" />
<input type="reset" class="btn btn-default" value="Reset" /></div></div>
</form>
</div><!--Panel Body Close-->
</div><!--Panel default Close-->
</div><!--End of Col6 Form-->
</div><!--End of form Row-->
</div><!--End of Container-->
d_submit.php
ob_start();
//global $target_path;
include_once($_SERVER["DOCUMENT_ROOT"].'/dashboard/other/crudfrm.php');
$shaobj = new shashter();
if(isset($_POST['submit']) ){
$name1 = mysql_real_escape_string($_POST['nametxt']);
if(isset($_POST['taksalitxt'])){
$taksali = true;
}
else {
$taksali = false;
}
if(isset($_POST['fproducttxt'])){
$fproduct = true;
}
else {
$fproduct = false;
}
$desc = mysql_real_escape_string($_POST['desctxt']);
//*************IMAGE MANIPULATION
if(isset($_FILES['upimage'])){
$errors= array();
$file_name = $_FILES['upimage']['name'];
$file_size =$_FILES['upimage']['size'];
$file_tmp =$_FILES['upimage']['tmp_name'];
$file_type=$_FILES['upimage']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['upimage']['name'])));
$extensions = array("jpeg","jpg","png");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
// Place it into your "uploads" folder mow using the move_uploaded_file() function
$moveResult = move_uploaded_file($file_tmp, $_SERVER['DOCUMENT_ROOT'].'uploadimg/other/full/'.$file_name);
//echo $moveResult;
echo $_SERVER['DOCUMENT_ROOT']."/uploadimg/other/thumb/".$file_name."<br>";
//exit();
echo "<br>Success";
}else{
print_r($errors);
}
}
}/*isset condition close*/
$thumb_path = "thumb/".$file_name;
$full_path = "full/".$file_name;
//*************IMAGE MANIPULATION END
if($moveResult) {
echo "File Uploaded Successfull!";
}else{
echo "ERROR: File not uploaded. Try again.";
// unlink($file_tmp); // Remove the uploaded file from the PHP temp folder
// exit();
}
//unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder
// ---------- Include Universal Image Resizing Function --------
include_once($_SERVER["DOCUMENT_ROOT"]."/dashboard/image_resize_lib.php");
$target_file = $_SERVER['DOCUMENT_ROOT']."/uploadimg/other/full/$file_name";
$resized_file = $_SERVER['DOCUMENT_ROOT']."/uploadimg/other/thumb/$file_name";
$wmax = 200;
$hmax = 150;
//echo "<br><br>../../uploadimg/shashter/full/".$file_name;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
// ----------- End Universal Image Resizing Function -----------
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";
//#################################### SAVE RECORD USING CLASS LIBRARY ####################################
$register = $shaobj->createpro($name1, $desc, $taksali, $fproduct, $thumb_path, $full_path );
if($register){
// if everything is ok, try to upload file
echo "<script>alert('Product Added Successful')</script>";
header('Location: http://example.com/dashboard/admindashboard.php');
exit();
}else{
echo "<script>alert('Product Not Added Successful')</script>";
}
Add one check inside your if condition and put your code inside try catch block so that you will get the proper error messages. Try with this and edit as per your need :).
try {
if (isset($_POST['submit'])) {
if (isset($_FILES['upimage'])) {
$errors = array();
$file_name = $_FILES['upimage']['name'];
$file_size = $_FILES['upimage']['size'];
$file_tmp = $_FILES['upimage']['tmp_name'];
$file_type = $_FILES['upimage']['type'];
$file_ext = strtolower(end(explode('.', $_FILES['upimage']['name'])));
$extensions = array("jpeg", "jpg", "png");
if (in_array($file_ext, $extensions) === false) {
$errors[] = "extension not allowed, please choose a JPEG or PNG file.";
}
if ($file_size > 2097152) {
$errors[] = 'File size must be excately 2 MB';
}
if (empty($errors) == true) {
// Place it into your "uploads" folder mow using the move_uploaded_file() function
$dirPath = $_SERVER['DOCUMENT_ROOT'] . '/uploadimg/other/full/';
if (!file_exists($dirPath)) {
mkdir($dirPath, 0777, true);
}
$moveResult = move_uploaded_file($file_tmp, $dirPath . $file_name);
//echo $moveResult;
echo $dirPath . $file_name . "<br>";
//exit();
echo "<br>Success";
} else {
print_r($errors);
}
}
}
} catch (Exception $ex) {
echo $ex->getMessage();
}
Hope this will help you.
Sorry questions I might be confusing but this should reveal that I can help from the teacher teacher here. This script is part of the social network socialkit script.
How to keep the chat function directly upload images could serve as the messaging menu.
<div class="chat-textarea">
<textarea class="auto-grow-input" onfocus="SK_focusChat();" onkeyup="SK_sendChatMessage(this.value,<?php echo $sk['chat']['recipient']['id']; ?>,event);"></textarea>
<div class="advanced-options">
<i class="icon-smile cursor-hand" onclick="javascript:$('.chat-emoticons-wrapper').toggle();"></i>
<div class="chat-emoticons-wrapper">
<?php
$emoticons = SK_getEmoticons();
if (is_array($emoticons)) {
foreach ($emoticons as $emo_code => $emo_icon) {
echo '<img src="' . $emo_icon . '" width="16px" onclick="addEmoToChat(\'' . $emo_code . '\');">';
}
}
?>
</div>
</div>
<input class="message-photo-input hidden" name="photos[]" type="file" accept="image/jpeg,image/png" onchange="SK_uploadMessageForm();">
<div class="options-wrapper" style="float:left ; margin-top:5px">
<i class="icon-camera progress-icon cursor-hand" title="<?php echo $lang['upload_photo']; ?>" valign="middle" onclick="$('.message-photo-input').click();"></i>
</div>
</div>
<?php
}
?>
</div>
</div>
And this is the javascript on the chat.
<script>
function SK_sendChatMessage(text,recipient_id,e) {
document.title = document_title;
textarea_wrapper = $('.chat-textarea');
chat_messages_wrapper = $('.chat-messages');
if (e.keyCode == 13 && e.shiftKey == 0) {
e.preventDefault();
textarea_wrapper.find('textarea').val(''); chat_messages_wrapper.append('<div class="chat-text align-right temp-text" align="right"><div class="text-wrapper float-right">' + text + '<div class="marker-out"><div class="marker-in"></div></div></div><div class="float-clear"></div></div>');
$.post(SK_source() + '?t=chat&a=send_message', {text: text, recipient_id: recipient_id}, function (data) {
chat_messages_wrapper
.append(data.html)
.scrollTop(chat_messages_wrapper.prop('scrollHeight'))
.find('.temp-text')
.remove();
});
}
}
// upload photo chat
function SK_uploadMessageForm() {
document.title = document_title;
$('chat_messages_wrapper').submit();
SK_progressIconLoader($('.chat-textarea').find('.options-wrapper'));
}
</script>
It javascript to be able to send picture messages
function SK_sendMessageForm(e) {
document.title = document_title;
if (e.keyCode == 13 && e.shiftKey == 0) {
e.preventDefault();
$('form.send-message-form').submit();
SK_progressIconLoader($('.textarea-container').find('.options-wrapper'));
}
}
function SK_uploadMessageForm() {
document.title = document_title;
$('form.send-message-form').submit();
SK_progressIconLoader($('.textarea-container').find('.options-wrapper'));
}
It send the form of a message
<div class="textarea-container" align="center">
<form class="send-message-form" method="post" enctype="multipart/form-data">
<textarea class="message-textarea auto-grow-input" name="text" placeholder="<?php echo $lang['write_a_message_label']; ?>..." onkeyup="SK_sendMessageForm(event);" onfocus="SK_sendMessageForm(event);" data-height="22" disabled></textarea>
<input class="message-photo-input hidden" name="photos[]" type="file" accept="image/jpeg,image/png" onchange="SK_uploadMessageForm();">
<div class="options-wrapper">
<i class="icon-camera progress-icon cursor-hand" title="<?php echo $lang['upload_photo']; ?>" valign="middle" onclick="$('.message-photo-input').click();"></i>
</div>
<input name="timeline_id" value="<?php echo $sk['user']['id']; ?>" type="hidden">
<input id="recipient-id" name="recipient_id" value="0" type="hidden">
</form>
</div>
</div>
</div>
My questions, this code upload image not work on chat.
<input class="message-photo-input hidden" name="photos[]" type="file" accept="image/jpeg,image/png" onchange="SK_uploadMessageForm();">
<div class="options-wrapper" style="float:left ; margin-top:5px">
<i class="icon-camera progress-icon cursor-hand" title="<?php echo $lang['upload_photo']; ?>" valign="middle" onclick="$('.message-photo-input').click();"></i>
</div>
I look forward to help you all.