-3

file is not uploading into my server, I am using move_uploaded_file function, i got output as Your file was uploaded successfully. but file is not thare in that location.

image.html

<script>
$(document).ready(function(){
    $(document).on('change', '#profile_photo', function(){
        var file_data = $('#profile_photo').prop('files')[0]['name']; 
        var email_id=document.getElementById('email_id').value;
        var filename=document.getElementById('profile_photo').files[0]['name'];
        form_data.append('file', document.getElementById('profile_photo').files[0]);

        $.ajax({
                url:"https://imageupload.php",
                method:"POST",
                data:({'file':filename,'email_id':email_id }),
                beforeSend:function(){
                    $('#uploaded_image').html("<label class='text-success'>file Uploading...</label>");
                },   
                success:function(data) {
                    $('#uploaded_image').html(data);
                }
            });
        });
});
</script>

imageupload.php

<?php
$filename=$_POST['file'];
$email_id=$_POST['email_id'].substr($filename, -5);

if(file_exists("upload/" . $email_id)){
    echo  $filename . " is already exists.";
} else{
    move_uploaded_file($filename, "upload/" .  $email_id);
    echo $email_id."Your file was uploaded successfully.";
}
?>
RiggsFolly
  • 83,545
  • 20
  • 96
  • 136
vidya devi
  • 229
  • 2
  • 13
  • 1
    Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** – RiggsFolly May 15 '19 at 08:03
  • You should start with the manual: https://www.php.net/manual/en/features.file-upload.post-method.php – jeroen May 15 '19 at 08:04
  • There is more to using FormData to send files using js than naming a variable `form_data` – RiggsFolly May 15 '19 at 08:08

1 Answers1

-1

imageupload.php Try Code below and make sure you set uploads folder permission to 777.

<?php
$filename=$_POST['file']['name'];
$tmppath = $_FILES['file']['tmp_name'];

$email_id=$_POST['email_id'].substr($filename, -5);
if(file_exists("upload/" . $email_id)){
    echo  $filename . " is already exists.";
} else{
    move_uploaded_file($tmppath, "upload/".$email_id);
    echo $email_id."Your file was uploaded successfully.";
}
?>
RiggsFolly
  • 83,545
  • 20
  • 96
  • 136
Raman
  • 604
  • 5
  • 12