上传文件后,我想通过将今天的日期追加到最后来重命名。
以下是将今天的日期放在文件的开头的示例:
move_uploaded_file($_FILES['imagefile']['tmp_name'], "docs/".$upload_date.$_FILES['imagefile']['name']);
但是如果我尝试将它添加到最后,它会在文件的扩展名后添加 例如“testdoc.pdf2014-05-29”
但我想: “testdoc 2014-05-29.pdf”
如何在文件名和扩展名之间插入今天的日期(或任何变量)?
答案 0 :(得分:2)
试试这个:
//split file name with "."
$fileName = explode(".", $_FILES['imagefile']['name']);
//remove extension from file name and save it in variable
$extension = array_pop($fileName);
//join back the array without extension
$fileName = implode(".", $fileName);
//get new file name by appending upload date and extension
$newFileName = $fileName . ' ' . $upload_date . '.' . $extension;
move_uploaded_file($_FILES['imagefile']['tmp_name'], "docs/".$newFileName);
你可以通过pathinfo
阅读它来实现同样的目的并自己尝试。
答案 1 :(得分:1)
$pathParts = pathinfo($_FILES['imagefile']['name']);
$fileNameExtension = $pathParts['extension'];
move_uploaded_file($_FILES['imagefile']['tmp_name'], "docs/" . $pathParts['filename'] . ' ' . $upload_date . '.' . $fileNameExtension);