问题描述
我有一个使用 imgur.com API 和jQuery的图像上传器.ajax()函数将图像上传到其服务器.但是,如果我使用表单的<input type="file"/>
元素浏览图像,则只有在相同目录中找到与page.php
文件相同的图像文件时,该图像才能成功上传图像找到该表格(如下所示). 如何允许表单从计算机上的任何目录上载图像?
I have an image uploader that uses the imgur.com API and jQuery's .ajax() function to upload images to their servers. However, if I browse for an image using the <input type="file"/>
element of the form, it will only be successful in uploading an image if the image file is found in the same directory as the page.php
file that the form is found in (shown below). How can I allow the form to upload images from any directory on my computer?
page.php:
<form action="page.php" method="post">
<input type="file" name="doc" id="doc" /><br/>
<input type="image" src="go.gif" name="submit" id="submit" />
</form>
推荐答案
您忘记了form
标记上的enctype="multipart/form-data"
属性.否则,文件上传通常会无法正常工作.
You've forgotten the enctype="multipart/form-data"
attribute on your form
tag, for one. Without that, file uploads generally don't work too well.
除此之外,服务器实际上并不关心您要从哪个目录上传,特别是在PHP下.在服务器上上传的副本存储有临时文件名($_FILES['file']['tmp_name']
),该文件名与计算机上的目录/文件名完全无关.
Beyond that, the server won't really care what directory you're uploading FROM, especially under PHP. The uploaded copy on the server is stored with temporary filename ($_FILES['file']['tmp_name']
) that has absolutely nothing to do with the directory/filename on your computer.
一旦它在服务器上,您就必须将该临时文件实际移到其他地方,因为一旦脚本终止并且您自己没有处理过,PHP会自动删除它. move_uploaded_file()
通常是用来完成该过程的.
Once it's on the server, you'll have to actually move that temporary file somewhere else, as PHP will auto-delete it once the script terminates and you've not handled it yourself. move_uploaded_file()
is what's generally used to take of that process.
这篇关于HTML上载表格将仅上载在PHP文件目录中找到的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!