更改上传图片名称中的字符数量? 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
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;