<form method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="upload">
</form>
How can I get the uploaded file without saving it and how can I display it?
<form method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="upload">
</form>
How can I get the uploaded file without saving it and how can I display it?
use $_FILES['image']
to retrive the image.
<?php
session_start();
if(isset($_FILES['image'])){
$file_tmp_name =$_FILES['image']['tmp_name'];
$str = file_get_contents($file_tmp_name);
$b64img=base64_encode($str);
$_SESSION['image'] = $b64img; // holds your image string in session without saving it.
}
Here is how you can do it with jquery
. look at this:
$("#imgInp").change(function(){
readURL(this);
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<img id="blah" src="images/defaultUser.jpg" alt="your image"/>
<input type='file' id="imgInp" accept="image/*"/>
<?php
if(isset($_POST["submit"])) {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>