Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP file upload, sending formdata object
    primarykey
    data
    text
    <p>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.</p> <p>Here is my code:</p> <p>profile.php</p> <pre><code>&lt;form role="form" id="editUserProfile" method="post" action="" enctype="multipart/form-data"&gt; &lt;input type="file" id="filename" name="filename" class="file-input" accept="image/*"/&gt;&lt;/a&gt; &lt;button type="submit"&gt;Save&lt;/button &lt;/form&gt; </code></pre> <p>javascript.js</p> <pre><code> $('#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); } }) } }); </code></pre> <p>And than I want to check in some other php (script.php) script if file was uploaded.</p> <pre><code>if(is_uploaded_file($_FILES['filename']['tmp_name']) &amp;&amp; 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'] &lt; $maxsize ){ $dbh = new PDO("mysql:host=localhost;dbname=test", 'root'); $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $dbh-&gt;prepare("INSERT INTO table (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)"); $stmt-&gt;bindParam(1, $type); $stmt-&gt;bindParam(2, $imgfp, PDO::PARAM_LOB); $stmt-&gt;bindParam(3, $size); $stmt-&gt;bindParam(4, $name); $stmt-&gt;execute(); }else{ throw new Exception("File Size Error"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload