[英]how to rename uploaded image file in php
I want to rename every image file i upload. 我想重命名我上传的每个图像文件。 I am working for a e-commerce site, i want to upload each image with their product name and ID from database. 我在一个电子商务网站上工作,我想从数据库上传每个图像及其产品名称和ID。
Product name should be like (4124-01.jpg) at the end and upload its name to the database. 产品名称的末尾应类似于(4124-01.jpg),并将其名称上传到数据库。 where 4124 is product name and 01 is ID . 其中4124是产品名称,01是ID。
I am new in PHP and stackoverflow both. 我是PHP和stackoverflow的新手。
Thank you in advance. 先感谢您。
here's a little code:::::::: 这是一个小代码:::::::::
if(is_uploaded_file($_FILES['image']['tmp_name'])){
$allowed=array('image/jpg','image/jpeg','image/gif','image/x-png','image/png');
if(in_array($_FILES['image']['type'],$allowed)){
echo 'Allowed format. Wait a little for completion of this task.';
}else{
$errors[]='Invalid format of image file. Please select a jpg, jpeg or png file.';
} // End of if(in_array($_FILES['image']['type'],$allwed)){
$temp='../products/'.($_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'],$temp)){
$product_name=$_FILES['image']['name'];
echo '<p>'.$product_name .'has been uploaded';
echo '<img src="../products/'.$product_name.'" width="300" height="300">';
}else{
$errors[]='The image could not be moved. Check your directories.';
} // End of if(move_uploaded_file($_FILES['image']['tmp_name'],$temp)){
}else{
$errors[]='You did not select a product image.';
} // End of if(is_uploaded_file($_FILES['image']['tmp_name'])){
Any help would be appreciated. 任何帮助,将不胜感激。
If you already have the product's id and name, that should be the value of your $temp
variable. 如果您已经有了产品的ID和名称,则该值应为$temp
变量的值。
You can get the extension with the following code: 您可以使用以下代码获取扩展名:
$extension = strtolower(substr(strrchr($_FILES['image']['name'], '.'), 1));
And concatenated with your desired name. 并与您所需的名称连接。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.