Php 上载时重命名多个图像
我正在尝试重命名上传的多个图像。我可以上传并保存到一个文件夹,但我似乎无法重命名它们。下面是我的代码和我尝试的内容:Php 上载时重命名多个图像,php,Php,我正在尝试重命名上传的多个图像。我可以上传并保存到一个文件夹,但我似乎无法重命名它们。下面是我的代码和我尝试的内容: $targetDir = "uploads/"; $allowTypes = array('jpg','png','jpeg','gif'); $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; if(!empty(array_filter($_FILES['file
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['files']['name']))){
foreach($_FILES['files']['name'] as $key=>$val){
$fileName = basename($_FILES['files']['name'][$key]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
move_uploaded_file($_FILES["files"]["tmp_name"][$key],"uploads/".$fileName.time());
//move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath);
}else{
$errorUploadType .= $_FILES['files']['name'][$key].', ';
}
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
这是因为你甚至都没有尝试重命名它们。你必须用这行代码做一些更巧妙的事情$targetFilePath=$targetDir$文件名代码>是,没有发布全部代码。对不起,请参见上文编辑我尝试过的内容。我在$filename的末尾添加了一个时间变量,但这是在扩展名中添加了数字。
<?php
$targetDir = "uploads/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
if(!empty(array_filter($_FILES['files']['name']))){
foreach($_FILES['files']['name'] as $key=>$val){
$fileName = basename($_FILES['files']['name'][$key]);
$expFileName=explode(".",$fileName);
$fileName=rand().".".$expFileName[1];
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
move_uploaded_file($_FILES["files"]["tmp_name"][$key],$targetFilePath);
//move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath);
}else{
$errorUploadType .= $_FILES['files']['name'][$key].', ';
}
}
}else{
$statusMsg = 'Please select a file to upload.';
}
// Display status message
echo $statusMsg;
?>