上传文件时is_uploaded_file返回false?

时间:2016-07-18 16:47:59

标签: php file-upload

我使用简单的输入文件类型将pdf上传到服务器:

<form action="subirCircular.php" method="post" enctype="multipart/form-data">

     <input type="file" name="userfile" accept="application/pdf">
     <br><br>

  <button type="submit" class="btn btn-default">Subir</button>
</form>

我在php中收到上传文件:

<?php
 define ("FILEREPOSITORY","./uploads/");

   if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

      if ($_FILES['userfile']['type'] != "application/pdf") {
         echo "<p>Class notes must be uploaded in PDF format.</p>";
      } else {
         $name = $_POST['name'];
         $result = move_uploaded_file($_FILES['userfile']['tmp_name'], FILEREPOSITORY."/$name.pdf");
         if ($result == 1) echo "<p>File successfully uploaded.</p>";
         else echo "<p>There was a problem uploading the file.</p>";
      } #endIF
   }else{
       echo 'ERROR!';
       }


?>

事情是条件永远不会被调用,我总是得到一个错误的'is_uploaded_file'。

我想知道我做错了什么,谢谢!

2 个答案:

答案 0 :(得分:1)

我宁愿使用:

    import {Component, NgModule} from '@angular/core'
    import {BrowserModule} from '@angular/platform-browser'

    import { Component } from '@angular/core';

    @Component({
      selector: 'my-app',
      templateUrl: './app.html'
    })
    export class AppComponent {
      title = 'csvTOjson works!';
      text  : any ;
      JSONData : any;
      csvJSON(csvText) {
       var lines = csvText.split("\n");

       var result = [];

       var headers = lines[0].split(",");
       console.log(headers);
       for (var i = 1; i < lines.length-1; i++) {

           var obj = {};
           var currentline = lines[i].split(",");

           for (var j = 0; j < headers.length; j++) {
               obj[headers[j]] = currentline[j];
           }

           result.push(obj);

       }

       //return result; //JavaScript object
       console.log(JSON.stringify(result)); //JSON
       this.JSONData = JSON.stringify(result);
    }

     convertFile(input) {

     const reader = new FileReader();
     reader.readAsText(input.files[0]);
     reader.onload = () => {
       let text = reader.result;
       this.text = text;
       console.log(text);
       this.csvJSON(text);
     };

    }
    }


    @NgModule({
      imports: [ BrowserModule ],
      declarations: [ AppComponent ],
      bootstrap: [ AppComponent ]
    })
    export class AppModule {}

检查一切是否正常,如果是,那么我使用

$_FILES['userfile']['error']

移动上传的文件。 到目前为止它的确有效。

答案 1 :(得分:-1)

检查你的请求方法 - 它应该是POST,而不是PUT / PATCH /...