简体   繁体   English

Angular + PHP文件上传(无法发送新创建的属性)

[英]Angular + PHP file upload (can't sent newly created properties)

I'm using ng-file-upload . 我正在使用ng-file-upload It works perfectly, But I need to add some specific properties to object that I sent. 它运行完美,但是我需要向发送的对象添加一些特定的属性。 Here controller method: 这里的控制器方法:

self.uploadFiles = function (files) {
  if (files && files.length) {
    for (var i = 0; i < files.length; i++) {
      files[i].upload_folder = 'upload'; //here i add first property
      files[i].current_date = getCurrentDate(); //here second
      var file = files[i];
      console.log(files[i]);
      Upload.upload({
        url: 'app/views/antonovich/upload.php',
        headers: {'Content-Type': undefined},
        method: 'POST',
        data: file,
        file: file
      }).progress(function(evt){
        self.uploadProgress = parseInt(100.0 * evt.loaded / evt.total);
        //console.log('progress: ' + self.uploadProgress + '% ' + evt.config.file.name);
      }).success(function(data, status, headers, config){
        console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
        $('.errors').html(data);
        self.uploadReady = config.file.name + ' uploaded.';
      });
    }
  }
}

In console log I see that properties, for example: 在控制台日志中,我看到了这些属性,例如:

File {$$hashKey: "object:19", upload_folder: "upload", current_date: "2016-01-08"}
$$hashKey: "object:19"
current_date: "2016-01-08"
file: File
lastModified: 1448878298580
lastModifiedDate: Mon Nov 30 2015 13:11:38 GMT+0300 (Russia Standard Time)
name: "14488072165280.png"
size: 872713
type: "image/png"
upload_folder: "upload"
webkitRelativePath: ""
__proto__: File

PHP script uploads these files to folder and creates DB row, But 2 created properties are undefind, and return just this: PHP脚本将这些文件上传到文件夹并创建数据库行,但是未定义2个创建的属性,并仅返回以下内容:

Response: Array
(
    [name] => 14488072165280.png
    [type] => image/png
    [tmp_name] => C:\wamp\tmp\phpC39D.tmp
    [error] => 0
    [size] => 872713
)

here php script example: 这里的PHP脚本示例:

<?php
  if(isset($_FILES['file'])) {
    print_r($_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'];
    $upload_folder = $_FILES['file']['upload_folder'];
    $current_date = $_FILES['file']['current_date'];
    print_r($upload_folder);
    print_r($current_date);

    $db = new PDO('mysql:host=localhost;dbname=antonovich', 'root', '');
    $query = "INSERT INTO  portfolio(url,date) VALUES('$file_name','$current_date')";
    $stmt = $db->prepare($query);
    $stmt->execute();

    $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
    move_uploaded_file($file_tmp,"../../img/".$upload_folder."/".$file_name);
    echo $file_name . " uploaded file: " . "images/" . $file_name;

  }
  else {
    $errors= array();
    $errors[]="No image found";
    print_r($errors);
  }
?>

For background, see PHP documentation on file uploads . 有关背景,请参阅有关文件上传的PHP文档 The $_FILES superglobal only contains a fixed set of values about each uploaded file ( name , type , tmp_name , error , and size ), any other data from the POST request should be in the $_POST superglobal instead. $ _FILES超全局变量仅包含有关每个上载文件的一组固定值( nametypetmp_nameerrorsize ),而来自POST请求的任何其他数据都应位于$ _POST超全局变量中。 Try changing your PHP script like so: 尝试像这样更改您的PHP脚本:

$upload_folder = $_POST['upload_folder'];
$current_date = $_POST['current_date'];

As a side note, I would be wary about allowing clients to specify the destination folder without robust validation. 附带说明一下,对于允许客户端在没有可靠验证的情况下指定目标文件夹,我会有所警惕。 Attackers might be able to use relative paths to upload malicious code. 攻击者可能能够使用相对路径来上传恶意代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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