限制上传文件的名称
我正在使用以下代码供用户上传图像。
if ((($_FILES["companylogofile"]["type"] == "image/jpeg")
|| ($_FILES["companylogofile"]["type"] == "image/pjpeg"))
&& ($_FILES["companylogofile"]["size"] < 75000))
{
if ($_FILES["companylogofile"]["error"] > 0)
{
echo "Return Code: " . $_FILES["companylogofile"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["companylogofile"]["name"] . "<br />";
echo "Type: " . $_FILES["companylogofile"]["type"] . "<br />";
echo "Size: " . ($_FILES["companylogofile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["companylogofile"]["tmp_name"] . "<br />";
if (file_exists("Template/" . $_FILES["companylogofile"]["name"]))
{
echo $_FILES["companylogofile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["companylogofile"]["tmp_name"],
"Template/" . $_FILES["companylogofile"]["name"]);
}
}
}
else
{
echo "Invalid file";
}
如您所见,我只接受上传 .jpeg 文件,但有大小限制。我还需要做的是对它们上传的文件的名称添加限制。基本上,我只想允许上传名为“Logo”的文件。有没有办法限制上传的文件的名称?
谢谢!
I am using the following code for user to upload an image.
if ((($_FILES["companylogofile"]["type"] == "image/jpeg")
|| ($_FILES["companylogofile"]["type"] == "image/pjpeg"))
&& ($_FILES["companylogofile"]["size"] < 75000))
{
if ($_FILES["companylogofile"]["error"] > 0)
{
echo "Return Code: " . $_FILES["companylogofile"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["companylogofile"]["name"] . "<br />";
echo "Type: " . $_FILES["companylogofile"]["type"] . "<br />";
echo "Size: " . ($_FILES["companylogofile"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["companylogofile"]["tmp_name"] . "<br />";
if (file_exists("Template/" . $_FILES["companylogofile"]["name"]))
{
echo $_FILES["companylogofile"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["companylogofile"]["tmp_name"],
"Template/" . $_FILES["companylogofile"]["name"]);
}
}
}
else
{
echo "Invalid file";
}
As you can see I only accept .jpeg files to be upload with a restriction on size. What I also need to do is add a restriction to the name of the file they are upload. Basically I want to only allow files to be uploaded if they are named say: Logo. Is there a way to put a restriction on the name of the files being uploaded?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在寻找“Logo.jpg”,可以通过以下方式实现
if you're looking exactly for "Logo.jpg", it is achievable by
是的,肯定有一种方法可以限制字符串格式。我建议您研究使用 preg_match 通过 PHP 使用 PERL 正则表达式 http://php .net/manual/en/function.preg-match.php
如果您以前没有使用过正则表达式,它们只是通过压缩语法测试字符串格式的一种方法。可以在这里找到开始使用正则表达式的好地方:http: //www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
Yes there is definitely a way to put a restriction on string formats. I would recommend you look into using PERL regular expressions via PHP using preg_match http://php.net/manual/en/function.preg-match.php
If you haven't used regular expressions before, they are simply a way to test against string formatting via a condensed syntax. A GREAT place to get started with regular expressions can be found here: http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
什么构成“坏”文件“Logo3?logo-with-some-long-rambling-filename?
仅供参考:不要使用 $_FILES 数组中的
['type']
值进行验证这是用户提供的文件的 mime 类型 - 可以轻松更改它以欺骗您。 rel="nofollow">getimagesize()
使服务器确定图像的类型。What constitutes a 'bad' file" Logo3? logo-with-some-long-rambling-filename?
Just FYI: Don't use the
['type']
value in the $_FILES array for validation. That's the mime-type of the file as provided by the user - it can be easily changed to lie to you. Use something likegetimagesize()
to make the server determine the image's type.是的,只需测试
$_FILES['companylogofile']['name']
。示例:但是,为什么要对上传设置如此烦人的限制呢?我希望你不会认为这会让它变得安全或者什么?
Yes, just test
$_FILES['companylogofile']['name']
. Example:But, why would you want to put such an annoying restriction on the upload? I hope you're not thinking it will make it ore secure or anything?