如何让PHP和AJAX上传的图像路径? [英] how to get the uploaded image path in php and ajax?
问题描述
我有一个简单的形式:
<输入类型=隐藏名称=MAX_FILE_SIZE值=2097152ID =MAX_FILE_SIZE>
<输入类型=文件名称=file_uploadID =file_upload级=picture_main>
<输入类型=提交名称=upload_pictureID =upload_pictureVALUE =提交>
我试图以一个ajax上传:
submit.on(点击,函数(){
var文件= $('#file_upload)VAL()。
uploadImageAjax(文件);
返回false;
});
VAR uploadmageAjax =功能(文件)
{
$阿贾克斯({
键入:POST,
网址:/测试/索引/ imageupload
数据: {
文件:文件
},
数据类型:JSON,
成功:功能(数据){
的console.log(数据);
}
});
}
我得到的回复是,例如,文件:C:\ fakepath \ weirdan003-10.jpeg
但林不知道这是什么 fakepath
是!?
如果我要做到这一点在PHP只有我会得到这样的形象:
如果(使用isset($ _ POST ['upload_picture'])){
$数据= $ formImageUpload->的GetValues();
$ pictureName = $数据['画面'];
....
,然后上传。
所以我想弄清楚的是,如果Ajax调用POST的该行动的权利文件,这样我就可以把它上传到磁盘。
将 $('#file_upload)VAL();
持有$ _FILE ??
什么想法?
感谢
var文件= $('#file_upload)VAL()。
这将只返回一个路径,客户端计算机上的文件。对于SECURIT原因,它就像回到C:\ fakepath \ file_name.ext。如果我没有记错,在一些旧的浏览器有可能得到一个真实路径。但尽管如此,它不会帮助您获得服务器上的文件。
有关AJAX风格的上传,你可以使用一些插件,你有建议。或者只是使用 jQuery的形式插件。它会工作非常类似于 $。阿贾克斯
。
现在,当文件正确上传,你会发现在 $这一切需要的信息_文件
在你的情况下,它会像 $ _ FILES ['file_upload']
其中file_upload'是你的文件输入一个名称。
现在你可以从临时存储移动与 move_uploaded_file 一>。做任何你想要的文件
编辑: 我看你是使用Zend。看看<一href="http://ahsangill.word$p$pss.com/2009/02/17/zend-framework-file-upload-using-zend_form_element_file/"相对=nofollow>这个有关如何使用的FileUpload元素在服务器端运行。与Zend您可以使用文件上传方法而不是move_uploaded_file
i have a simple form:
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" id="MAX_FILE_SIZE">
<input type="file" name="file_upload" id="file_upload" class="picture_main">
<input type="submit" name="upload_picture" id="upload_picture" value="Submit">
i am trying to to an ajax upload:
submit.on("click", function(){
var file = $('#file_upload').val();
uploadImageAjax(file);
return false;
});
var uploadmageAjax = function(file)
{
$.ajax({
type: "POST",
url: "/test/index/imageupload",
data: {
'file': file
},
dataType: "json",
success: function (data) {
console.log(data);
}
});
}
what i get back is, for example, file: "C:\fakepath\weirdan003-10.jpeg"
but im not sure what that fakepath
is !?
if i were to do it in php only i would get the image like this:
if (isset($_POST['upload_picture']) ) {
$data = $formImageUpload->getValues();
$pictureName = $data['picture'];
....
and then upload it.
So what i want to figure out is if the ajax call POST's to that action the right file so i can then upload it to the disk.
will $('#file_upload').val();
hold the $_FILE??
any ideas?
thanks
var file = $('#file_upload').val();
This will return only a path to file on client machine. for securit reason it is returned like c:\fakepath\file_name.ext. If I remember correctly, in some older browsers it was possible to get a real path. But still, it does not helps you to get a file on server.
For ajax style upload you can use some of plugins you got recommended. Or just use jQuery Forms plugin. It will work very similar to $.ajax
.
Now, when file is uploaded correctly, you will find all required info about it in $_FILES
In your case it will be something like $_FILES['file_upload']
where 'file_upload' is a name of your file input.
Now you can move it from temporary storage with move_uploaded_file. And do whatever you want with that file
EDIT: And I see you are using Zend. Take a look at this about how to work with FileUpload element on the server side. With Zend you may use FileUpload methods instead of move_uploaded_file
这篇关于如何让PHP和AJAX上传的图像路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!