我正在使用此功能上传我的文件:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = $_FILES['Artwork']['name'];
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
如何使用自定义名称更改$imageName = $_FILES['Artwork']['name'];
,但在名称中保留文件扩展名,例如:myCustomName.jpg
?
谢谢!
答案 0 :(得分:7)
您需要在代码中修改的唯一一行是:
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
'CustomName'的位置。是图像所需的新名称。 pathinfo如果PHP函数用路径和文件名来处理操作。
您的整个代码将是:
if ((($_FILES["Artwork"]["type"] == "image/gif")
|| ($_FILES["Artwork"]["type"] == "image/jpeg")
|| ($_FILES["Artwork"]["type"] == "image/jpg")
|| ($_FILES["Artwork"]["type"] == "image/pjpeg"))
&& ($_FILES["Artwork"]["size"] < 20000000))
{
if ($_FILES["Artwork"]["error"] > 0)
{
//echo "Return Code: " . $_FILES["Artwork"]["error"] . "<br />";
}else{
$imageName = 'CustomName.' . pathinfo($_FILES['Artwork']['name'],PATHINFO_EXTENSION);
move_uploaded_file($_FILES["Artwork"]["tmp_name"],
$path_image . $imageName);
}
}else{
//echo "invalid file";
}
答案 1 :(得分:1)
$ext = last(explode('.', $_FILES['Artwork']['name']));
$custom_name = 'something';
$imageName = $custom_name.'.'.$ext;
答案 2 :(得分:0)
我认为你太复杂了。它就像用点分割文件名并使用最后一个元素一样简单:
$parts = explode('.', $_FILES['Artwork']['name']);
$newname = "myCustomName" . (size($parts) > 1 ? '.' . last($parts) : '')