我需要使用Angular.js和PHP在单击按钮单击项目文件夹中存储不同类型的文档。我在下面解释我的代码。
var fileData={'image':file,'regdoc':regDocs,'compRegDoc':compRegDocs};
$scope.upload=Upload.upload({
url: 'php/uploadAll.php',
method:'POST',
file: fileData
}).success(function(data, status, headers, config) {
console.log('file',data);
}).error(function(data, status) {
console.log('err file',data);
})
uploadALL.php:
<?php
if(isset($_FILES['file'])){
$errors= array();
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
$extensions = array("jpeg","jpg","png");
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.";
}
if($file_size > 2097152){
header("HTTP/1.0 401 Unauthorized");
$errors[]='File size cannot exceed 2 MB';
}
if(empty($errors)==true){
//$today=('date')(new Date(),'yyyy-MM-dd HH:mm:ss');
move_uploaded_file($file_tmp,"../upload/".$file_name);
echo " uploaded file: " . "upload/" . $file_name;
}else{
print_r($errors);
}
}
else{
$errors= array();
header("HTTP/1.0 401 Unauthorized");
$errors[]="No image found";
print_r($errors);
}
?>
这里我有一个图像,另外两个是.pdf / docx类型文件。当用户单击提交按钮时,这3个文件应存储在upload folder
。
答案 0 :(得分:0)
//在php页面中使用此代码... 它用于上传所有文件
if(isset($_POST['submit'])!=""){
$name=$_FILES['file']['name'];
$size=$_FILES['file']['size'];
$type=$_FILES['file']['type'];
$temp=$_FILES['file']['tmp_name'];
$caption1=$_POST['caption'];
$link=$_POST['link'];
move_uploaded_file($temp,"upload/".$name);
}