douchao1957 2011-07-13 17:32
浏览 67

限制上传文件的名称

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!

  • 写回答

4条回答 默认 最新

  • duanji1482 2011-07-13 17:35
    关注

    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/

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算