Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you use <a href="https://github.com/danialfarid/angular-file-upload" rel="nofollow">ng-file-upload</a> You can do most of those pre-validation on the client side like checking the file size or type with ngf-max-size or ngf-pattern directives.</p> <p><code>Upload.upload()</code> will send a POST multipart/form-data request to the server so <code>$_FILES['file']</code> should contain the uploaded file.</p> <p>HTML</p> <pre><code>&lt;div ng-controller="MyCtrl"&gt; &lt;input type="text" name="username" ng-model="username"/&gt; &lt;input type="file" ngf-select="onFileSelect($file)" ngf-pattern="'image/*'" ngf-max-size="2M"&gt; &lt;/div&gt; </code></pre> <p>JS:</p> <pre><code>//inject angular file upload directives and service. angular.module('myApp', ['ngFileUpload']); var MyCtrl = [ '$scope', 'Upload', function($scope, Upload) { $scope.onFileSelect = function(file) { if (!file) return; Upload.upload({ url: '/upload.php', data: {file: file, username: $scope.username} }).then(function(resp) { // file is uploaded successfully console.log(resp.data); }); }; }]; </code></pre> <p>upload.php</p> <pre><code>$fname = $_POST["fname"]; if(isset($_FILES['image'])){ //The error validation could be done on the javascript client side. $errors= array(); $file_name = $_FILES['image']['name']; $file_size =$_FILES['image']['size']; $file_tmp =$_FILES['image']['tmp_name']; $file_type=$_FILES['image']['type']; $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); $extensions = array("jpeg","jpg","png"); if(in_array($file_ext,$extensions )=== false){ $errors[]="image extension not allowed, please choose a JPEG or PNG file."; } if($file_size &gt; 2097152){ $errors[]='File size cannot exceed 2 MB'; } if(empty($errors)==true){ move_uploaded_file($file_tmp,"images/".$file_name); echo $fname . " uploaded file: " . "images/" . $file_name; }else{ print_r($errors); } } ?&gt; </code></pre>
    singulars
    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