我在使用Angular.js和PHP将文件上传到本地文件夹时遇到了一个问题。我正在解释下面的代码。
<div ng-class="{'myError': billdata.regdoc.$touched && billdata.regdoc.$invalid }">
<input type="file" class="filestyle form-control" data-size="lg" name="regdoc" id="regdoc" ng-model="regfile" ngf-pattern="application/pdf,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.wordprocessingml.document" accept="application/pdf,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.wordprocessingml.document" ngf-select="onRegFileSelect($file);" >
</div>
我的控制器端代码如下所示。
$scope.onRegFileSelect=function(files){
//console.log('docs details',files);
regDocURL=files;
$scope.firstDocs='';
}
if($scope.firstDocs ==''){
var regDocs=regDocURL;
var curnum=(Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
newRegPath=curnum+"_"+ regDocs.name;
Upload.rename(regDocs, newRegPath);
var regdocsdata={'image':regDocs};
fileData.push(regdocsdata);
}else{
newRegPath=$scope.firstDocs;
}
console.log('file',fileData);
$scope.upload=Upload.upload({
url: 'php/uploadAll.php',
method:'POST',
file: fileData
}).success(function(data, status, headers, config) {
console.log('file succ',data);
}).error(function(data, status) {
console.log('file error',data);
});
uploadAll.php:
<?php
if(isset($_FILES['file'])){
// print_r($_FILES['file']);
for($i=0;$i<count($_FILES['file']['name']);$i++){
print_r($_FILES['file']['size'][$i]['image']);
/*$file_name = $_FILES['file']['name'][$i]['image'];
$file_size =$_FILES['file']['size'][$i]['image'];
$file_tmp =$_FILES['file']['tmp_name'][$i]['image'];
$file_type=$_FILES['file']['type'][$i]['image'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$extensions = array("jpeg","jpg","png","pdf","ppt","docx");
if(in_array($file_ext,$extensions )=== false){
header("HTTP/1.0 401 Unauthorized");
$errors[]="image extension not allowed, please choose a JPEG or PNG file.";
print_r($errors);
return;
}
if($file_size > 2097152){
header("HTTP/1.0 401 Unauthorized");
$errors[]='File size cannot exceed 2 MB';
print_r($errors);
return;
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../upload/".$file_name);
echo " uploaded file: " . "upload/" . $file_name.$file_size;
}else{
print_r($errors);
}*/
}
}else{
$errors= array();
header("HTTP/1.0 401 Unauthorized");
$errors[]="No file found";
print_r($errors);
}
?>
这里我的问题是,当我上传大文件时,假设一个文件超过5/6 mb
显示0
的文件大小,以便我无法将该文件上传到文件夹中,但是当我上传时文件相关kb
文件大小很容易测量。另一个问题是上传大文件时它的真实文件没有上传到文件夹但是上面文件中的if(empty($errors)==true)
正在执行哪个不应该。这是我需要的计算文件大小并相应上传。请帮助我。