4

I want to upload a file, send it to javascript formData object and than via ajax to some php script which will put the file into database. It works fine when i upload a file in profile php, send it to formData object in javascript and send it back to profile.php. Problem is that when i want to send formData object to some other php script which isn't profile.php, it doesn't work.

Here is my code:

profile.php

<form role="form" id="editUserProfile" method="post" action="" enctype="multipart/form-data">
    <input type="file" id="filename" name="filename" class="file-input" accept="image/*"/></a>
    <button type="submit">Save</button
</form>

javascript.js

    $('#editUserProfile').validate({
        submitHandler: function (form) {
            var aFormData = new FormData();

            aFormData.append("filename", $('#filename').get(0).files[0]);

            $.ajax({
                type: "POST",
                url: "script.php",
                data: aFormData,
                success: function(data){
                    window.location.reload(true);
                }
            })
        }
    });

And than I want to check in some other php (script.php) script if file was uploaded.

if(is_uploaded_file($_FILES['filename']['tmp_name']) && getimagesize($_FILES['filename']['tmp_name']) != false){
    $size = getimagesize($_FILES['filename']['tmp_name']);
    $type = $size['mime'];
    $imgfp = fopen($_FILES['filename']['tmp_name'], 'rb');
    $size = $size[3];
    $name = $_FILES['filename']['name'];
    $maxsize = 99999999;

    if($_FILES['userfile']['size'] < $maxsize ){
        $dbh = new PDO("mysql:host=localhost;dbname=test", 'root');
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $dbh->prepare("INSERT INTO table (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)");

        $stmt->bindParam(1, $type);
        $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
        $stmt->bindParam(3, $size);
        $stmt->bindParam(4, $name);

        $stmt->execute();
    }else{
        throw new Exception("File Size Error");
    }
}
5
  • hard to follow exactly how you want things to work. Can you itemize each step starting at AJAX submit? Commented Oct 30, 2013 at 17:42
  • Ok. I have a aFormData object with file in it, and i want it to send to script.php where where i will check if the object with file recieved successfully. The only problem is to step into if statement. I tested this case with the script where the form is and the code works well, but i want to have a function somewhere else and that is the problem. Commented Oct 30, 2013 at 17:50
  • somewhere else is very vague...please update question with far more specifics of what you expect to happen... mention files in steps. Your descriptions are far too loose to follow Commented Oct 30, 2013 at 19:39
  • I have 2 php scripts: profile.php and upload.php. In profile.php, i have a form where file is uploaded. After uploading a file in form, I append this file to a javascript object formdata, like i descriped before(script.js). When object is loaded with filedata i want to send it via ajax to upload.php. In upload.php I am checking if($_FILES) is set and that's the main thing for me. The problem is that ($_FILES) isn't set after sending formdata object via ajax to upload.php. Commented Oct 30, 2013 at 21:46
  • I also tried to send formdata object back to profile.php and executed this part of code(checking if($_FILES) is set and inserting to databased) and it worked. So the problem is only of sending the object with file data to upload.php and checking if($_FILES) is set. Commented Oct 30, 2013 at 21:57

1 Answer 1

6

Added to jQuery.ajax and it works now:

processData: false,

contentType: false,
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.