Note that there are some explanatory texts on larger screens.

plurals
  1. POGet file name of file being uploaded and write it into a text field
    primarykey
    data
    text
    <p>I have a form with two input fields, one <code>type=file</code> and other <code>type=text</code>.</p> <p>I would like to:</p> <ol> <li>select a file with <code>type=file</code> field to be uploaded using php</li> <li>grab the <strong>file name</strong> of the file being uploaded</li> <li>insert the <code>upload dir/file_name</code> into the <code>type=text</code> field</li> </ol> <p>The file gets uploaded fine. The problem is i get the <code>file name</code> wrong, depends on the browser i'm using. </p> <ul> <li><strong>Firefox</strong> works fine and i get: <code>somepath/file_name.ext</code></li> <li><strong>IE(10)</strong> gets me full local file path: <code>somepath/C:\Users\User\Desktop\file_name.ext</code></li> <li><strong>Chrome</strong> gets all cute and returns: <code>somepath/C:\fakepath\file_name.ext</code></li> </ul> <p>How do i solve this? I need <strong>only</strong> the <code>file_name.ext</code>, like Firefox does it. Needless to say i need the file name detected and appropriate text field changed as the file is selected and before the form is submitted. </p> <p>The HTML part:</p> <pre><code>&lt;form enctype="multipart/form-data" action="" method="post"&gt; &lt;input name="data_up" id="data_up" type="file" /&gt;&lt;br&gt; &lt;input name="data" id="data" type="text" value="&lt;?php if(isset($_POST['submit'])) { echo $_POST['data']; } ?&gt;" /&gt; &lt;button name="submit" type="submit"&gt;Submit&lt;/button&gt; &lt;/form&gt; </code></pre> <p>The PHP part (simplified, needs security added):</p> <pre><code>if (isset($_POST['submit'])) { if ($_FILES["data_up"]["error"] == 0) { move_uploaded_file($_FILES["data_up"]["tmp_name"], "somepath/".$_FILES["data_up"]["name"]); } } </code></pre> <p>The JS part:</p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { $('#data_up').change(function () { var fileName = $(this).val(); $('#data').val('somepath/'+fileName); }); }); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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