如果来自同一用户,则替换保存文件
Replace save files if from same User
我有一个名为 resale_certificates 的目录,我将文件保存到其中,但使用他们的部分电子邮件和分配给他们的代码加密名称。
注意:每次加密都是相同的,但对每个用户都是唯一的!
当他们上传 image.png 时,它会将文件保存到 theirEncrypt.png
如果他们上传另一个 image.png 它将替换 theirEncrypt.png
但是,当他们上传 image.jpg 时,现在 resale_certificates 目录中将有 theirEncrypt.jpg 和 theirEncrypt.png。
处理此问题的最佳方法是什么?我正在寻求建议并愿意改变我的保存方式或我可以采取的技巧来防止这种情况发生!
谢谢!
如果您知道以前上传的图片的名称,那么您可以在保存新图片之前执行以下操作:
<?php
$previousImageName = 'theirEncrypt.png';
unlink(APP_DIR . "/resale_certificates/" . $previousImageName);
好吧,您可以使用图片库将他们上传的图片转换为您想要的任何格式,即如果他们上传 .JPG,您可以使用 Imagick 或 GD 等图片库输出 .PNG 文件并上传这些文件。
但是,如果您不介意 .JPG 或 .PNG(或 .GIF),您可以使用 PHP 扫描目录以查找所有文件(虽然可能非常密集!) 来查找具有给定名称的文件。
例如:
<?php
foreach( scandir('/path/to/resale_certificates/') as $file ){
if( $file != '.' && $file != '..'){
// explode so we remove the last extension path ( not type safe ! )
$arguments = explode('.', $file);
// store the last part
$ext = end($arguments);
// pop the extension from the $arguments array so we are left
// with whatever was left
array_pop($arguments);
// concatenate the $arguments into a single string again
$filename = implode('.', $arguments);
// now we can check the filename again
if( $filename == $theirEncrypt){
unlink('/path/to/resale_certificates/' . $filename . '.' . $ext);
}
}
}
编辑:
$file 是 scandir() 返回的 $files 数组中的一个字符串;功能。单点和双点是导航到当前 (.) 和父 (..) 目录的一种方式,因此是符号链接。另一种选择是检查 $file 是否实际上是一个文件。您可以将比较行替换为 is_file('/path/to/resale_certificates/' . $file) 以检查它是文件还是符号链接(如 . 和 .. ),但它更加密集检查字符串比较。在您的用例中,它不是必需的。
在相关说明中,这非常密集,具体取决于您拥有的客户端和证书的数量,作为替代方案,您可以将文件名存储到存储器(即数据库或类似的东西)并取消链接文件在那里找到,这将使您无需遍历每个文件并直接取消链接文件。
我有一个名为 resale_certificates 的目录,我将文件保存到其中,但使用他们的部分电子邮件和分配给他们的代码加密名称。 注意:每次加密都是相同的,但对每个用户都是唯一的!
当他们上传 image.png 时,它会将文件保存到 theirEncrypt.png 如果他们上传另一个 image.png 它将替换 theirEncrypt.png 但是,当他们上传 image.jpg 时,现在 resale_certificates 目录中将有 theirEncrypt.jpg 和 theirEncrypt.png。
处理此问题的最佳方法是什么?我正在寻求建议并愿意改变我的保存方式或我可以采取的技巧来防止这种情况发生!
谢谢!
如果您知道以前上传的图片的名称,那么您可以在保存新图片之前执行以下操作:
<?php
$previousImageName = 'theirEncrypt.png';
unlink(APP_DIR . "/resale_certificates/" . $previousImageName);
好吧,您可以使用图片库将他们上传的图片转换为您想要的任何格式,即如果他们上传 .JPG,您可以使用 Imagick 或 GD 等图片库输出 .PNG 文件并上传这些文件。
但是,如果您不介意 .JPG 或 .PNG(或 .GIF),您可以使用 PHP 扫描目录以查找所有文件(虽然可能非常密集!) 来查找具有给定名称的文件。
例如:
<?php
foreach( scandir('/path/to/resale_certificates/') as $file ){
if( $file != '.' && $file != '..'){
// explode so we remove the last extension path ( not type safe ! )
$arguments = explode('.', $file);
// store the last part
$ext = end($arguments);
// pop the extension from the $arguments array so we are left
// with whatever was left
array_pop($arguments);
// concatenate the $arguments into a single string again
$filename = implode('.', $arguments);
// now we can check the filename again
if( $filename == $theirEncrypt){
unlink('/path/to/resale_certificates/' . $filename . '.' . $ext);
}
}
}
编辑: $file 是 scandir() 返回的 $files 数组中的一个字符串;功能。单点和双点是导航到当前 (.) 和父 (..) 目录的一种方式,因此是符号链接。另一种选择是检查 $file 是否实际上是一个文件。您可以将比较行替换为 is_file('/path/to/resale_certificates/' . $file) 以检查它是文件还是符号链接(如 . 和 .. ),但它更加密集检查字符串比较。在您的用例中,它不是必需的。
在相关说明中,这非常密集,具体取决于您拥有的客户端和证书的数量,作为替代方案,您可以将文件名存储到存储器(即数据库或类似的东西)并取消链接文件在那里找到,这将使您无需遍历每个文件并直接取消链接文件。