[英]Angular JS file upload via php — Undefined index:
我正在尝试使用Angular和PHP制作上传方法。 那是我到目前为止所拥有的...
的HTML
<form class="well" enctype="multipart/form-data">
<div class="form-group">
<label for="file">Select a file to upload</label>
<input type="file" fileread="uploadme" >
<p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
</div>
<input type="button" ng-click="upoload()" class="btn btn-lg btn-primary" value="Upload">
</form>
JS
app.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
scope.$apply(function () {
scope.fileread = changeEvent.target.files[0];
});
});
}
}
}]);
app.controller('upController',function($scope,$http){
$scope.upoload=function(){
$http.post('server/uploadFile.php',$scope.uploadme).
success(function(data, status) {
alert(status +" -- "+ data)
})
}})
的PHP
<?php
//turn on php error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
//$data = json_decode( file_get_contents('php://input') );
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_FILES['uploadme']['name'];
$tmpName = $_FILES['uploadme']['tmp_name'];
$error = $_FILES['uploadme']['error'];
$size = $_FILES['uploadme']['size'];
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
switch ($error) {
case UPLOAD_ERR_OK:
$valid = true;
//validate file extensions
if ( !in_array($ext, array('jpg','jpeg','png','gif')) ) {
$valid = false;
$response = 'Invalid file extension.';
}
//validate file size
if ( $size/1024/1024 > 2 ) {
$valid = false;
$response = 'File size is exceeding maximum allowed size.';
}
//upload file
if ($valid) {
move_uploaded_file($tmpName,'../uploads/'.$name);
$response = 'OOOK!';
exit;
}
break;
case UPLOAD_ERR_INI_SIZE:
$response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
break;
case UPLOAD_ERR_FORM_SIZE:
$response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
break;
case UPLOAD_ERR_PARTIAL:
$response = 'The uploaded file was only partially uploaded.';
break;
case UPLOAD_ERR_NO_FILE:
$response = 'No file was uploaded.';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
break;
case UPLOAD_ERR_CANT_WRITE:
$response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
break;
case UPLOAD_ERR_EXTENSION:
$response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
break;
default:
$response = 'Unknown error';
break;
}
echo $response;
}
?>
问题是我找不到为索引变量赋索引的方法,我认为这就是我不断收到“未定义索引”错误消息的原因。 有什么建议么?
感谢您的帮助。
有几个原因导致您收到未定义的索引消息。
将您的表单代码更改为:
<form class="well" enctype="multipart/form-data" method="post">
上传文件时需要。
省略时,窗体默认为GET方法。
<input type="file" fileread="uploadme" >
它需要一个名称属性: name="uploadme"
将输入更改为:
<input type="file" fileread="uploadme" name="uploadme">
另外,由于您正在使用以下条件语句:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
它与POST方法结合使用,正如我所说的,您的表单中缺少该方法。
阅读关于move_uploaded_file()
的文档
从手册中引用:
此功能检查以确保filename所指定的文件是有效的上传文件(意味着该文件是通过PHP的HTTP POST上传机制上传的)。 如果文件有效,则将其移动到destination所给定的文件名。
您应该使用另一个库通过angular js上传文件,在这里看看: https : //github.com/danialfarid/angular-file-upload
然后您将在js控制器中使用类似的代码:
$scope.upload = $upload.upload({
url: 'your url', //upload.php script, node.js route, or servlet url
method: 'POST',
file: scope.fileread
}).progress(function (evt) {
//console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function (data) {
// file is uploaded successfully
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.