【问题标题】:HTML5 PHP AJAX upload: PHP not workingHTML5 PHP AJAX 上传:PHP 不工作
【发布时间】:2013-01-06 08:17:12
【问题描述】:

我正在尝试编写自己的 php 上传器,以便学习和理解它是如何工作的。尽量不使用任何库/插件/API。所以基本上我有 javascript 的东西做它应该是的(我希望..)。现在我被困在PHP上。我希望它上传并保存为 img/1.png。 (不使用任何变量来测试它......)。这是我的脚本。保持非常简单,没有任何错误检查,只是为了让基本的上传工作。这适用于多个文件吗?有人可以指导我只为非常基本的(多)文件上传我的 PHP 文件应该是什么样子吗?谢谢!

album_create.js

$(document).ready(function() {
  $('#album_create').submit(function() {
    var file = document.getElementById('album-files').files[0]; //Files[0] = 1st file
    var reader = new FileReader();

    reader.onload = function(f) {shipOff(f);};
    reader.onerror = function(event) {
      console.error("File could not be read! Code " + event.target.error.code);
    };
    reader.readAsText(file, 'UTF-8');
    return false;
  });
});


function shipOff(event) {
  var result = event.target.result;
  var fileName = document.getElementById('album-files').files[0].name; //Should be 'picture.jpg'
  console.log(fileName);
  $.post('ajax/album_create.php', { data: result, name: fileName }, function(data){console.log(data);});
  return false;
}

ajax/album_create.php

$data = $_POST['data'];

move_uploaded_file($data[0], 'img/1.png');

【问题讨论】:

    标签: php ajax html upload


    【解决方案1】:

    您正在发帖:

    data: result, name: fileName 
    

    但album_create.php 期待:

        $data = $_POST['data'];
        $fileName = $_POST['fileName'];
    

    $fileName = $_POST['fileName']; 应该是:$fileName = $_POST['name'];

    【讨论】:

    • 好收获。我将其更改为 $_POST['name'] 但它仍然不起作用。我实际上并没有在任何地方使用 $fileName 。我是否在 move_uploaded_file 函数中传递了不正确的值作为参数?我尝试将 $data 和 $data[0] 作为第一个参数,但它们都不起作用。
    • 使用 Firebug 控制台查看您发布的确切内容
    猜你喜欢
    • 2011-08-05
    • 2014-06-05
    • 1970-01-01
    • 2013-11-17
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    相关资源
    最近更新 更多