jquery PHP图像上传(使用.post方法)

时间:2011-10-09 16:40:05

标签: php jquery

我正在尝试在表单上上传图片,但我正在使用Jquery .Post函数来提交表单的数据。我得到一个未经解释的索引的PHP错误。这是我的代码的一小部分:

相关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()。我对该元素发出了警报,它只会提醒文件名而不是整个路径。

如何获得整条路径?

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

感谢!!!

4 个答案:

答案 0 :(得分:1)

您应该阅读有关file uploading using POST

的PHP文档

答案 1 :(得分: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

答案 2 :(得分:0)

出于安全原因,浏览器不包含完整路径。您可以控制服务器上文件的名称。

如果要异步上传,应该查看一些现有的jquery插件,例如:

http://www.uploadify.com/

http://www.phpletter.com/Demo/AjaxFileUpload-Demo/

答案 3 :(得分:0)

对于目标路径,您必须使用以下内容:

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
相关问题