[英]Uploading image with url in Codeigniter(with csv file)
我正在尝试使用 url 上传图片。 它工作正常。 图像可以正确上传,但如果图像已经存在,则它会覆盖文件。
这是我的代码 -
$url=$d[1];
$name = basename($url);
$my_location='././assets/temp_upload/';
if (!defined('IMAGE_DIR')) define('IMAGE_DIR', $my_location);
if(!$img = file_get_contents($url))
{
die('Getting that file failed');
}
if(!$f = fopen(IMAGE_DIR.'/'.$name, 'w'))
{
die('Opening file for writing failed');
}
if (fwrite($f, $img) === FALSE)
{
die('Could not write to the file');
}
fclose($f);
我只是想,如果图像文件已经存在,那么它的名称应该改变,这样图像就不会被覆盖。
请帮我。
谢谢。
很简单,你可以在图像文件名的末尾添加一个随机字符串。
$name_temp = basename($url);
$my_location='././assets/temp_upload/';
if (file_exists($my_location.''.$name_temp)) {
$fileinfo = pathinfo($url);
$name=$fileinfo['filename'].'_'.rand(1,10000).''.$fileinfo['extension'];
}
else
{
$name=$name_temp;
}
您可以根据需要自定义它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.