0
<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?

  • 4
    What have you tried? Where is the rest of the form's code, and the page that the data is sent to? – Ben Jun 03 '15 at 07:13
  • use ajax... http://api.jquery.com/jquery.ajax/ and take a look at: http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html – Luca Jun 03 '15 at 07:13
  • take a look to `$_FILES` – Alive to die - Anant Jun 03 '15 at 07:13
  • Once you post the form the file will be uploaded and it will be stored in the $_FILES array. Take a look on [php.net](http://php.net/manual/en/features.file-upload.php) – Jim Wright Jun 03 '15 at 07:14

3 Answers3

2

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.

}
Malay M
  • 1,659
  • 1
  • 14
  • 22
1

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/*"/>
Luthando Ntsekwa
  • 4,192
  • 6
  • 23
  • 52
0
<?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.";
    }
}
?> 
Qaisar Satti
  • 2,742
  • 2
  • 18
  • 36