PHP和Ajax文件上传 - 无法用$ _FILES的tmp_name的值 [英] PHP and Ajax file upload - Can't get the tmp_name with $_FILES

查看:208
本文介绍了PHP和Ajax文件上传 - 无法用$ _FILES的tmp_name的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传使用Ajax的文件,但我有麻烦处理文件... 出于测试目的,我已经建立一个简单的code,它是这样的:

I'm trying to upload a file using Ajax, but I'm having troubles handling the file... For test purposes I've build a simple code that looks like this:

记者:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_POST["q"];
echo $q;

它工作正常, xmlhttp.responseText 打印 [目标文件]

我的问题,然而,就是我需要用 $的临时文件名_ FILES [Q] [tmp_name的值'] 。要做到这一点,我已经改变了code以下内容:

My problem, however, is that I need to get the temporary file name with $_FILES["q"]['tmp_name']. To do so I have changed the code to the following:

记者:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("enctype","multipart/form-data");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_FILES["q"]["tmp_name"];
echo $q;

问题是,现在 xmlhttp.responseText 我没有得到任何东西。 任何人都知道我在做什么错了?

Problem is that now with xmlhttp.responseText I don't get anything. Anyone knows what I'm doing wrong?

推荐答案

看看这个答案让文件上传使用AJAX。这是可能的,但在所有的浏览器不兼容。

Check out this answer for making file uploads with AJAX. It is possible, but not compatible in all browsers.

<一个href="http://stackoverflow.com/questions/4856917/jquery-upload-progress-and-ajax-file-upload/4943774#4943774">jQuery上传进度和AJAX文件上传

-

另外,如果你想在飞行上传,有一个很酷的图书馆,你可以得到所谓Uploadify。这是一个闪光/ jQuery的(或HTML5现在)钻井平台,允许你上传的飞行文件。在Flash版本,我最后一次使用它...您可以在回调函数添加到让它做你想要什么本质

Alternatively, if you want on the fly uploads, there is a cool library you can get called 'Uploadify'. It's a flash/jquery (or HTML5 now) rig that allows you to upload files on the fly. In the flash version, last time I used it... you can add in callback functions to make it do essentially anything you want.

一些聪明的JavaScript可以使这项工作给你。

Some clever javascript could make this work for you.

http://www.uploadify.com/

这篇关于PHP和Ajax文件上传 - 无法用$ _FILES的tmp_name的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆