As I Have 2 Div Boxes. One is Input and the other one is Output Div Box as Shown in the Image Below.:
Now What I am doing is I am uploading the Image using the Help of PHP. And uploading same image on two folders named Input and Output.
What I want is When I Click Submit Button the image from input folder should be shown on Input Box and the Output Folder Image to be shown In Output Folder.
I am able to show the Input folder Image but not able to show the Output folder Image in output Div.
Here is my HTML Code :
<div class="container">
<div class="row">
<div class="col-md-6">
<h2 class="inputImage-text text-center">Input Image</h2>
<form id="uploadForm" action="upload.php" method="post" enctype="multipart/form-data">
<div id="targetLayer">No Image</div>
<div id="uploadFormLayer">
<input name="fileToUpload" type="file" class="inputFile" /><br />
<input type="submit" value="Submit" class="btnSubmit btn btn-primary" />
</div>
</form>
</div>
<div class="col-md-6">
<h2 class="outputImage-text text-center">Output Image</h2>
<div class="outputDiv">
</div>
</div>
</div>
</div>
Here is my php Script:
<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) {
$sourcePath = $_FILES['fileToUpload']['tmp_name'];
$targetPath = "input/".$_FILES['fileToUpload']['name'];
$outputImage = "output/".$_FILES['fileToUpload']['name'];
if(move_uploaded_file($sourcePath,$targetPath)) {
copy($targetPath, $outputImage)
?>
<img class="image-preview" src="<?php echo $targetPath; ?>" class="upload-preview" />
<?php
}
}
}
?>
and Here is the AJAX Code :
<script type="text/javascript">
$(document).ready(function(e) {
$("#uploadForm").on('submit', (function(e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function(data) {
$("#targetLayer").html(data);
},
error: function() {}
});
}));
});
</script>
Use dataType option to accept the response in JSON format.
<script type="text/javascript">
$(document).ready(function (e) {
$("#uploadForm").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
dataType: "json",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data)
{
$("#targetLayer").html(data.input_file);
$(".outputDiv").html(data.output_file);
},
error: function()
{
}
});
}));
});
</script>
Return both input and output files in an array format as follows
<?php
$uploadedFiles = [];
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) {
$sourcePath = $_FILES['fileToUpload']['tmp_name'];
$targetPath = "input/".$_FILES['fileToUpload']['name'];
$outputImage = "output/".$_FILES['fileToUpload']['name'];
if(move_uploaded_file($sourcePath,$targetPath)) {
copy($targetPath, $outputImage);
$uploadedFiles['input_file'] = '<img class="image-preview" src="'.$targetPath.'" class="upload-preview" />';
$uploadedFiles['output_file'] = '<img class="image-preview" src="'.$outputImage.'" class="upload-preview" />';
}
}
}
echo json_encode($uploadedFiles);
?>
Refer to this documentation regarding dataType
Add this with in your script
$(".outputDiv").html(data);
I have changed your php, html and jquery. You need to add jsonarray instead of html because it would be easy to put multiple data in json array and can get it easily in jquery .
In html i have added img tag to display image in output.
In php script, I have removed html and added json array for both the images.
In jquery script, I have replaced all img tag with src.
<div class="container">
<div class="row">
<div class="col-md-6">
<h2 class="inputImage-text text-center">Input Image</h2>
<form id="uploadForm" action="upload.php" method="post" enctype="multipart/form-data">
<div id="targetLayer">No Image <img src="" id="intput-file-view"/></div>
<div id="uploadFormLayer">
<input name="fileToUpload" type="file" class="inputFile" /><br/>
<input type="submit" value="Submit" class="btnSubmit btn btn-primary" />
</div>
</form>
</div>
<div class="col-md-6">
<h2 class="outputImage-text text-center">Output Image</h2>
<div class="outputDiv">
<img src="" id="output-file-view"/>
</div>
</div>
</div>
</div>
<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) {
$sourcePath = $_FILES['fileToUpload']['tmp_name'];
$targetPath = "input/".$_FILES['fileToUpload']['name'];
$outputImage = "output/".$_FILES['fileToUpload']['name'];
if(move_uploaded_file($sourcePath,$targetPath)) {
copy($targetPath, $outputImage);
echo json_encode(array("inputImage"=>$targetPath,"outputPath"=>$outputImage));
exit;
}
echo json_encode(array("inputImage"=>"","outputPath"=>""));
exit;
}
}
?>
<script type="text/javascript">
$(document).ready(function (e) {
$("#uploadForm").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
var response=JSON.parse(data);
if(response.inputImage != "" && response.outputPath != ""){
$("#intput-file-view").attr("src",response.inputImage);
$("#output-file-view").attr("src",response.outputPath);
}
},
error: function()
{
}
});
}));
});
</script>
Related
file upload works with ajax and php, but does not return responses! in the upload.php page is displayed:
uploaded succesfully!
html:
<form action="upload.php" method="post" enctype="multipart/form-data" id="form">
<div style="padding: 15px;">
<input type="file" name="file" id="file">
</div>
<div style="padding: 15px">
<input type="submit" value="Upload" id="upload">
</div>
</form>
ajax:
$(document).ready(function () {
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success: function (response) {
alert(response);
}
})
})
})
php:
if (isset($_FILES['file'])) {
$file_destination = "uploads/IMG-" . date("Y-m-d") . time() . ".jpg";
if (move_uploaded_file($_FILES['file']['tmp_name'], $file_destination)) {
echo "uploaded succesfully!";
}
}
issue exist on client side use this html and java script code hope your issue will be resolved
<form action="#" enctype="multipart/form-data" id="form">
<div style="padding: 15px;">
<input type="file" name="file" id="file">
</div>
<div style="padding: 15px">
<button value="Upload" id="upload">Submit</button>
</div>
</form>
js part
$(document).ready(function () {
$('#upload').on('click', function (e) {
e.preventDefault();
var formData = new FormData();
formData.append('file', $('#file')[0].files[0]);
$.ajax({
url: "upload.php",
type: "POST",
data: formData,
contentType: false,
// cache: false,
processData: false,
success: function (response) {
alert(response);
}
});
}); });
i just wanted to upload an image using ajax and codeigniter. Getting error which is saying "You did not select a file to upload." even i did select that image.
I recently tried many examples from many sources but did not get my answer. following i show you my code this is giving me the result as "You did not select a file to upload." when i already selected the file.
This is my view:
<div class="col-lg-6">
<div class="dropify-wrapper mb-4 form-group col-lg-6">
<div class="dropify-loader"></div>
<div class="dropify-errors-container">
<ul></ul>
</div>
<input class="dropify" name="userfile" id="input-file-now" type="file">
<button class="dropify-clear" type="button">Remove</button>
<div class="dropify-preview">
<span class="dropify-render"></span>
<div class="dropify-infos">
<div class="dropify-infos-inner">
<p class="dropify-filename"><span class="file-icon"></span>
<span class="dropify-filename-inner"></span></p>
<p class="dropify-infos-message">Drag and drop or click to replace</p>
</div>
</div>
</div>
</div>
</div>
This is ajax code:
$('#submit_btn').click(function (e) {
e.preventDefault();
var data = $('#formname').serialize();
$.ajax({
type: 'ajax',
method: 'post',
url: '<?php echo base_url() ?>ControllerName/functionName',
data: data,
enctype: 'multipart/form-data',
async: false,
dataType: 'json',
success: function (response) {
alert(response);
},
error: function () {
}
});
});
This is controller code:
public function functionName()
{
$config=[
'upload_path'=>'./Assets/imgs/users/',
'allowed_types'=>'jpg|png|gif'
];
$this->load->library('upload',$config);
if($this->upload->do_upload(')){
$data=$this->upload->data();
$image_path= base_url("Assets/imgs/users/".$data['raw_name'].$data['file_ext']);
$result= $image_path;
}else{
$result=$this->upload->display_errors('','');
}
}
Please add tag in your html and also use formdata in jquery code like below.
HTML Code:
<form id="form" method="post" enctype="multipart/form-data">
</form>
Jquery Code:
data: new FormData(formid),
here i am doing multiple image upload , user select the images and click the submit button means i want add watermark in that images after that i have to move the images in temporary folder,except adding water into the image i have completed, i don't know i how to add water mark in image , if anyone know means please update my answer
$("#rentBtnSubmit").click(function(e){
e.preventDefault();
var formData = new FormData();
var formData = new FormData($('#rentForm')[0]);
formData.append('file[]', $('input[type=file]')[0].files[0]);
$.ajax({
type:'POST',
url :"test.php",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data) {
if(data['status'] =='success'){
alert('Success');
}
else{
alert('Error');
}
},
error:function(exception){
$("#rentError").show();
$("#rentError").fadeOut(4000);
}
});
});
<form id="rentForm">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Upload Property Photo</label>
<br>
<div id="maindiv">
<div id="formdiv">
<span id="filediv col-md-3"><input name="file[]" type="file" id="file" multiple=""/></span>
</div>
</div>
</div>
</div>
</div><br><br>
<button type="button center-block" id="rentBtnSubmit">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
test.php
$FilleAll = $_FILES['file']['name'];
$fileCount = count($FilleAll);
for ($i = 1; $i < count($FilleAll); $i++) {
$extension = pathinfo( basename($_FILES['file']['name'][$i]), PATHINFO_EXTENSION);
$image_filename= md5(basename($_FILES['file']['name'][$i]).time()).'.'.$extension;
$move = move_uploaded_file($_FILES['file']['tmp_name'][$i],"watermark_images/".$image_filename);
}
I upload images through ajax method in my yii2 app. I can choose multiple files to upload but I need to upload max 5 files. How can I do that?
My view file:
<div class="row">
<div class="col-lg-6">
<p class="add_photo"><img class="describe_images" src="photo.png"></img>Добавить фото(до 5 штук)</p>
<form enctype="multipart/form-data"
"method"="post" "id"="my-form">
<input name="media[]" type="file" multiple id ="gallery-photo-add"/>
<input class="button" type="submit" alt="Upload" value="Upload" />
</form>
</div>
<div class="col-lg-6 pixels-line">
<div class="preview"></div>
</div>
</div>
ajax method which upload a files
<script>
$('#my-form').submit( function(e) {
e.preventDefault();
var data = new FormData(this); // <-- 'this' is your form element
$.ajax({
url: 'site/index',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
});
</script>
I want to get the temp name of image upload using php and ajax.I got the file name of upolad image. But dont get the temp name of image upload.My code is given below
main.php
<form action=" " method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="button" class="btn btn-default"
name="Import" value="Import" onclick="file_up()">
Upload</button>
</form>
<div id="upload_show"></div>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function file_up() {
var file = $('#file').val();
$.ajax({
type: "POST",
url: 'first_ajax.php',
data: 'file=' + file,
success: function (msg){
$("#upload_show").html(msg);
}
});
}
</script>
first_ajax.php
<?php
echo $file1 = $_POST['file']; // for get file name
echo $file1 = $_POST['file']['tmp_name']; //for get temp name
?>
Instead of,
var file = $('#file').val();
try,
var file = $("#file").prop("files")[0];
Use FormData to get $_FILES and $_POST array in AJAX file...
function file_up() {
var formData = new FormData($('#form')[0]);
$.ajax({
url: 'first_ajax.php',
type: 'POST',
data: formData,
async: false,
success: function(data) {
$("#upload_show").html(msg);
},
cache: false,
contentType: false,
processData: false
});
}