限制上传文件的名称

发布于 2024-11-24 03:23:59 字数 1162 浏览 2 评论 0原文

我正在使用以下代码供用户上传图像。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷路的信 2024-12-01 03:24:00
if (!preg_match("/logo/i", $_FILES["companylogofile"]["name"])) die("File have to contain 'logo' in name");

如果您正在寻找“Logo.jpg”,可以通过以下方式实现

if ("Logo.jpg" != $_FILES["companylogofile"]["name"]) die("File have to contain 'logo' in name");
if (!preg_match("/logo/i", $_FILES["companylogofile"]["name"])) die("File have to contain 'logo' in name");

if you're looking exactly for "Logo.jpg", it is achievable by

if ("Logo.jpg" != $_FILES["companylogofile"]["name"]) die("File have to contain 'logo' in name");
孤芳又自赏 2024-12-01 03:24:00

是的,肯定有一种方法可以限制字符串格式。我建议您研究使用 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/

三生池水覆流年 2024-12-01 03:24:00

什么构成“坏”文件“Logo3?logo-with-some-long-rambling-filename?

function isValidFileName($filename) {
   ... what do you want here?
}

if (!isValidFileName($_FILES['companylogoimage']['name'])) {
   die("Ooops, bad filename");
}

仅供参考:不要使用 $_FILES 数组中的 ['type'] 值进行验证这是用户提供的文件的 mime 类型 - 可以轻松更改它以欺骗您。 rel="nofollow">getimagesize() 使服务器确定图像的类型。

What constitutes a 'bad' file" Logo3? logo-with-some-long-rambling-filename?

function isValidFileName($filename) {
   ... what do you want here?
}

if (!isValidFileName($_FILES['companylogoimage']['name'])) {
   die("Ooops, bad 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 like getimagesize() to make the server determine the image's type.

挽你眉间 2024-12-01 03:24:00

是的,只需测试 $_FILES['companylogofile']['name']。示例:

if (false === strpos(strtolower($_FILES['companylogofile']['name']), 'logo')) {
    echo 'Error!';
    unlink($_FILES['companylogofile']['tmp_name'];
    return;
}

但是,为什么要对上传设置如此烦人的限制呢?我希望你不会认为这会让它变得安全或者什么?

Yes, just test $_FILES['companylogofile']['name']. Example:

if (false === strpos(strtolower($_FILES['companylogofile']['name']), 'logo')) {
    echo 'Error!';
    unlink($_FILES['companylogofile']['tmp_name'];
    return;
}

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文