Home > Article > Web Front-end > How to Determine File Size Before Upload Using AJAX and PHP?
Determine File Size Prior to Upload
In the context of file uploading, users often encounter the need to ascertain the file size before initiating the upload process. This is particularly relevant when file size limitations exist or when optimizing upload times is crucial.
Solution
Using AJAX and PHP in the change event of an input file field, it's possible to determine the file size. Implement the following approach:
Code Example
For example, consider the following HTML input file field:
<code class="html"><input type="file" id="myFile" /></code>
You can attach an event listener to this field using jQuery as follows:
<code class="javascript">$('#myFile').bind('change', function() { //this.files[0].size gets the size of your file. alert(this.files[0].size); });</code>
This code snippet will display the file size in bytes in an alert dialog.
The above is the detailed content of How to Determine File Size Before Upload Using AJAX and PHP?. For more information, please follow other related articles on the PHP Chinese website!