-1

I am uploading image through ajax and during the ajax call, I want to set tmp name of uploaded file, but it returns undefined.

This is my code:

function imageIsLoaded(e) {
  $("#file").css("color", "red");
  $('#image_preview').css("display", "block");
  $('#previewing').attr('src', e.target.result);
  //$('#previewing').attr('src', imagetempname);
  $('#previewing').attr('width', '150px');
  $('#previewing').attr('height', '150px');
  //alert(e.target.result);
};

$("#file").change(function() {
  $("#message").empty(); // To remove the previous error message
  var file = this.files[0];
  var imagefile = file.type;
  var imagename = file.name;
  var imagetempname = file.tmp_name;
  var match = ["image/jpeg", "image/png", "image/jpg"];
  if (!((imagefile == match[0]) || (imagefile == match[1]) || (imagefile == match[2]))) {
    $('#previewing').attr('src', 'noimage.png');
    $("#message").html("<p id='error'>Please Select A valid Image File</p>" + "<h4>Note</h4>" + "<span id='error_message'>Only jpeg, jpg and png Images type allowed</span>");
    return false;
  } else {
    var reader = new FileReader();
    reader.onload = imageIsLoaded;
    reader.readAsDataURL(this.files[0]);
    document.getElementById("imagename").value = imagename;

    alert(imagetempname);
    document.getElementById("imagetmpname").value = imagetempname;
  }
});

Here when I alert(imagetempname);, it will return undefined.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
suh
  • 19
  • 1
  • 7
  • Where is `imageIsLoaded` defined ? – Rayon Mar 21 '16 at 10:38
  • Please indent your code. – Jolta Mar 21 '16 at 10:41
  • What do you expect ```file.temp_name``` to be? temp_name is an attribute in php that is available when image is uploaded in temp directory. You are trying to get what? You haven't upload file and this is not available is javascript. It is server side (available in php). You can upload image using ajax and send the temp_file to client as response of ajax request. – Ali Farhoudi Mar 21 '16 at 10:47
  • function imageIsLoaded(e) { $("#file").css("color","red"); $('#image_preview').css("display", "block"); $('#previewing').attr('src', e.target.result); //$('#previewing').attr('src', imagetempname); $('#previewing').attr('width', '150px'); $('#previewing').attr('height', '150px'); //alert(e.target.result); }; – suh Mar 21 '16 at 10:56
  • this is my image is loaded function – suh Mar 21 '16 at 10:56
  • @ali. i want to use move_uploaded_file function in my php file for that i want to send temp name to my php file – suh Mar 21 '16 at 10:58
  • @suh You can't send it to your PHP. Actually you do not have it to send. You need to upload your file using ajax and it will be available in php. Here is a nice example: [jQuery ajax upload](http://stackoverflow.com/a/21758151/6006686) – Ali Farhoudi Mar 21 '16 at 11:03
  • Where is your AJAX call? I don't see it... – Mr. Polywhirl Mar 21 '16 at 11:05
  • @ali well i tried to upload file using formdata since 2 days but it doesn't work so i am trying this alternative. – suh Mar 21 '16 at 11:18
  • http://stackoverflow.com/questions/36102121/data-not-retrive-through-formdata/36102233?noredirect=1#comment59849703_36102233 – suh Mar 21 '16 at 11:18
  • I posted an answer in your old question page. Check if out. That is your way to do what you want to do. – Ali Farhoudi Mar 21 '16 at 12:38
  • You can't do that in javascript – fractefactos Feb 17 '17 at 04:15

1 Answers1

0

Your issue is that you're trying to access the file from the $_GET superglobal instead of the $_FILES superglobal where it would be held.

print_r($_FILES);

There is also an issue with your ajax.

function change(val){
$.ajax({
    url:"filecheck.php?id=" + val,
    type: "post",
    data: $('#yourformid').serialize()
    success:function(res){
        alert(res);
    }
});}

Otherwise you'd just check like this:

if(!empty($_FILES)) {

$name = $_FILES['file']['name'];
$tmp = $_FILES['file']['tmp_name'];}

NOTE

Look at code-jaffs comment that verifies my point above stating that your files won't upload the way you are currently doing this.