0
<input type="file" id="file" name="file[]" multiple/>

html

var uploadfile = $('#file').val();

$.ajax({
      type: 'POST',
      url: '../include/addimage.php',
      dataType: "text",
      data: {
        file: uploadfile
      },
      success: function(data) {
            alert(data);
      }error: function(data) {
            alert(data);

        }

Ajax Request JS

If(0 < ($_FILES["file"]['size']){
echo "file is set"

}else{
echo "No file"
}

Php code

The code above is my code to test if a file is ready to be uploaded in database. I always get No file even if i put a file in input text

UPDATE

Based on the sample from here

$fileName = $_FILES['file']['name'];
$fileType = $_FILES['file']['type'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileContent = file_get_contents($_FILES['file']['tmp_name']);
$fp = fopen($fileName, 'rb'); // read binary

i get the file using the code above and i turn the file into blob. But i get fopen(whomovedmycheese - Copy.pdf): failed to open stream: No such file or directory in in line $fp = fopen($fileName, 'rb'); // read binary. What seems to be the problem here?

Update

i used $_FILES['file']['tmp_name'] and it is ok now

Community
  • 1
  • 1
Brownman Revival
  • 3,620
  • 9
  • 31
  • 69
  • 1
    You defined `POST` as your type of sending `data` which means you need to use `$_POST['file']` in your php file in order to be able to access it. – kidA Feb 21 '15 at 09:33
  • i just changed it i tried `if(isset($_POST['file']))` still same result.. If empty `No file` when i put something in it `No file` will still show – Brownman Revival Feb 21 '15 at 09:35
  • 2
    @kidA — That isn't how file uploads work in PHP. – Quentin Feb 21 '15 at 09:51
  • Skip the uppercase `I` in your PHP `if` (consistency). – Mouser Feb 21 '15 at 09:52
  • @Quentin, who says the problem lays in the jQuery. I think it's bugged in the PHP, which this duplicate doesn't directly answer. – Mouser Feb 21 '15 at 09:57
  • @Quentin that question and mine is the same i wasnt able to see that question so i asked. The question is a bit old is there any new way to this or still the same as before? – Brownman Revival Feb 21 '15 at 10:05
  • @Quentin, I know but question has been changed since I posted previous comment. – kidA Feb 21 '15 at 10:08
  • @Mouser — I can see the jQuery code and it is sending the text of the file name instead of the file. I can also see the PHP, which is fine. – Quentin Feb 21 '15 at 11:41
  • @HogRider — The accepted answer on the duplicate was last updated less than a year ago. It's correct. – Quentin Feb 21 '15 at 11:42
  • @HogRider — Oh, and the most recent answer (which gives a complete and simple jQuery example) on the dupe is only a month and 2 days old! – Quentin Feb 21 '15 at 11:54
  • When in doubt check the inspector! I don't think you're sending file data at all. Use the FormData Api, maybe you'll need to construct it manually and append each file's data to it. – olanod Feb 23 '15 at 04:36
  • @Quentin i've seen the answer you were referring to but when running that example it has an error and i am not also sure if it was successfully upload since there was no confirmation also i dont see any jquery plugin that were is there so i can replicate his sample.. – Brownman Revival Feb 24 '15 at 02:46
  • The answer that I was talking about (from vickisys) doesn't use any plugins. As olanod said, you can use the Inspector in your browser to see if the request worked or not. – Quentin Feb 24 '15 at 08:59
  • @olanod that is what i am thinking as well that is why i want to ask how to send that file from input to ajax i tried using FormData but since i am not familiar i have trouble with using it until i am trying to figure out how to do the proper way if you can shed more light to this question please do so – Brownman Revival Feb 24 '15 at 10:27

0 Answers0