doubipiao1611 2014-02-25 16:03
浏览 15
已采纳

更改上传图片名称中的字符数量? SUBSTR

I have a site that you can upload an image. It works great but the uploaded image names are way too long - "swag_526abccf5e2bcd27d2000e3f9.jpg" I would like to shorten them to about 8 characters after the "swag_" part.

   function SaveUploadFile($file, $dir, $resize=TRUE, $maxw=0, $maxh=0, $quality=75)
{
    if(!$GLOBALS['image_verification']) $resize = FALSE;

    if ($file['tmp_name'])
    {
        $dotpos = strrpos($file['name'], ".");

        if ($dotpos) $ext = strtolower(substr($file['name'], $dotpos));
        else $ext = "";

        $newname = uniqid("swag_") . substr(md5($file['name']), 5, 12) . $ext;

        if ($resize && ($ext==".jpg" || $ext==".jpeg" || $ext==".jfif")) $copysuccess = SaveResizedJPG($file['tmp_name'], "$dir/$newname", $maxw, $maxh, $quality);
        else $copysuccess = copy($file['tmp_name'], "$dir/$newname");

        if ($copysuccess)
            $ret = $newname;
        else
            return "";

        unlink($file['tmp_name']);
        return $ret;
    }
    else
    {
        return "";
    }

I think it has to do with the numbers 5 and 12 after newname ?

$newname = uniqid("swag_") . substr(md5($file['name']), 5, 12) . $ext;

, but I don't understand how to change this to get the results I want. Thank you for your help

  • 写回答

1条回答 默认 最新

  • dongshan1959 2014-02-25 16:08
    关注
    substr(md5($file['name']), 5, 12) 
    

    this means, calculate the md5 sum of the $file['name'] and then starting from character 5, take the next 12 characters and use them as a filename.

    if you want to change the ammount of characters used in an image name, just play around with those numbers. start from lowering the 12, maybe to an 8 like this:

    substr(md5($file['name']), 5, 8) 
    

    maybe even less, try it out or else how are you going to learn about that stuff :-) this should help out as well:

    http://www.php.net/manual/en/function.substr.php

    edit: okay I just saw that you are using uniqid. you could remove that md5 part altogether if you only want 8 characters:

    $newname = substr(uniqid("swag_"), 0, 13) . $ext;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真