我正在用 php (codeigniter) 开发一个站点框架,并希望在图像上传上引入图像版本控制,以便我可以利用图像缓存。最简单的方法就是对图像进行 md5 并将其用作文件名,但我不喜欢这种方法,原因如下:
1)Not SEO friendly on the image names
2)md5 hashes seem unnecessarily long - and therefore larger database field required.
所以我正在考虑使用如下方法:
文件名以输入的图像名称开头,并使用下划线而不是空格,然后添加一个随机生成的整数,例如 8 位数字长。这意味着我必须检查该名称的现有图像,然后重新生成整数(如果存在)(尽管不太可能)。
大概我还必须为每个图像大小使用唯一的文件名,所以我想这里的解决方案是添加表示文件大小的前缀。
现在我想第一次就把这个做对,因为一旦部署框架,改变就会很痛苦,所以我真的只是在寻找输入
a)Whether my concerns are justified (particularly does the filename do anything for SEO and does the length of a random string of numbers affect it)
b)Whether there is anything else I should be concerned about or check for with my proposed approach.
c)Is there an easier approach, perhaps a hashing algorithm which produces much shorter results.
d) Is there already a ci lib out there that does this?
感谢您的意见和建议
最佳答案
这回答了您的一些问题:
用下划线替换空格不足以获得干净的文件名,因为您需要检查更多奇怪的字符,但您可以使用 CI 安全库中的 sanitize_filename() 方法: http://ellislab.com/codeigniter/user-guide/libraries/security.html
如果您确实想保留原始文件名,那么您的方法对我来说听起来不错。不过,通过简单的增量循环检查该结尾的文件是否存在,可以将文件名末尾的 8 位整数替换为“-1”、“-2”、“-3”。
文件上传库是您可以查看的东西 - http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html 。它很灵活,可以配置为保留原始文件名。从安全库获取 sanitize_filename() 来工作应该完全符合您的需要。
在我的所有 CI 应用程序中,我总是使用加密的文件名(此可选功能由 CI 文件上传类提供)。同时,我可以通过向库添加数字(如果未使用加密)或仅为其提供另一个加密名称(当加密选项打开时)来将库配置为不覆盖现有文件。我确实喜欢这种方式,因为它可以保持文件名一致干净(虽然很长并且不利于 SEO,但是 ALT 标记使其更多地暴露在搜索引擎中)。
关于php - 图像版本控制 - 最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915466/