2011-10-09 56 views
0

我想上传图像的形式,但我使用JQuery的.Post函数为了提交表单的数据。我得到一个未定义索引的PHP错误。这里是我的代码的一小部分:jQuery的PHP图像上传(使用.post方法)

相关HTML:

<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
Picture: <input name="uploadedfile" id="uploadedfile" type="file" /> 

相关的jQuery

$.post("registerCB.php", { 
    uploadedfile: $("#uploadedfile").val() 
} 

处理该提交的PHP:

//file upload 
     $uploadedfile= $_POST["uploadedfile"]; 



     /*--------------------Image Uploads-------------------------*/ 
     // Where the file is going to be placed 
     $target_path = "userImages/"; 

     /* Add the original filename to our target path. 
     Result is "uploads/filename.extension" */ 
     $target_path = $target_path . basename($_FILES[$uploadedfile]['name']); 

出现错误: enter image description here

结论: 我认为问题是图像输入上的.val()。我对那个元素做了一个警告,它只会提醒文件名不是整个路径。

我该如何获得整个路径?

一件事---- 我想控制文件的名称。所以无论用户上传什么,我都可以控制名字....这可能吗?

谢谢!!!

+1

我不是100%肯定,但$( “#UploadedFile的”)。VAL()应返回的文件名,而不是文件数据。 – neworld

回答

0

对于目标路径,你必须使用如下:

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
1

你不能读取<input type='data'/>的值,然后通过jQuery发布一些信息。 你需要通过jQuery发布表格<input type='data'>就在!

<form type='post' id='myForm' enctype="multipart/form-data"> 
<input type='file' name='uploadedFile'/> 
</form> 


$("myForm").attr("action", "registerCB.php").submit(); 

和关于通过PHP的数据的读取,我会refer to the php.net article

+1

表单应具有enctype =“multipart/form-data”属性。 – erenon