简体   繁体   English

通过php上传Angular JS文件-未定义索引:

[英]Angular JS file upload via php — Undefined index:

I'm trying to make an upload method with Angular and PHP. 我正在尝试使用Angular和PHP制作上传方法。 that is what i have so far.... 那是我到目前为止所拥有的...

HTML 的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 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

<?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;
    }
    ?>

The problem is that I can't find a way to give an index to the post variable, I think this is the reason I keep getting an "undefined index" error message. 问题是我找不到为索引变量赋索引的方法,我认为这就是我不断收到“未定义索引”错误消息的原因。 Any suggestions? 有什么建议么?

Thank you for the help. 感谢您的帮助。

There are a few reasons why you are getting an undefined index message. 有几个原因导致您收到未定义的索引消息。

  • One is that form requires a POST method. 一种是该表单需要POST方法。

Change your form code to: 将您的表单代码更改为:

<form class="well" enctype="multipart/form-data" method="post">

It is required when uploading files. 上传文件时需要。

Forms default to a GET method when omitted. 省略时,窗体默认为GET方法。

  • The other is this <input type="file" fileread="uploadme" > 另一个是<input type="file" fileread="uploadme" >

It needs a name attribute: name="uploadme" 它需要一个名称属性: name="uploadme"

Change your input to: 将输入更改为:

<input type="file" fileread="uploadme" name="uploadme">

Plus, since you are using this conditional statement: 另外,由于您正在使用以下条件语句:

if ($_SERVER['REQUEST_METHOD'] == 'POST')

it works in conjunction with the POST method, which as I said, is missing from your form. 它与POST方法结合使用,正如我所说的,您的表单中缺少该方法。


Read the documentation on move_uploaded_file() 阅读关于move_uploaded_file()的文档

Quoting from the manual: 从手册中引用:

This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). 此功能检查以确保filename所指定的文件是有效的上传文件(意味着该文件是通过PHP的HTTP POST上传机制上传的)。 If the file is valid, it will be moved to the filename given by destination . 如果文件有效,则将其移动到destination所给定的文件名。

You should use another library for uploading a file through angular js, take a look here: https://github.com/danialfarid/angular-file-upload 您应该使用另一个库通过angular js上传文件,在这里看看: https : //github.com/danialfarid/angular-file-upload

and then you`ll use it something like this in your js controller: 然后您将在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM