我有一个上传多个文件的表单。我使用的PHP代码工作正常,但我想重命名文件,不知道如何去做。我认为在名称上添加时间戳将是最好的答案。到目前为止,这是工作代码:
/* FILE UPLOAD CODE */
{
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . '/uploads/'; //set upload directory
/**
* we get a $_FILES['file'] array ,
* we procee this array while iterating with simple for loop
* you can check this array by print_r($_FILES['file']);
*/
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
$number_of_file_fields++;
if ($_FILES['file']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['file']['name'][$i];
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $_FILES['file']['name'][$i])) {
$number_of_moved_files++;
}
}
}
}
/* END FILE UPLOAD CODE */
任何有关添加内容以及在何处完成此操作的帮助将不胜感激。
答案 0 :(得分:0)
您可以将新文件名指定为move_uploaded_file()
中的第二个参数例如:
move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . "new_file_name");
答案 1 :(得分:0)
如果要为所有上传的文件指定相同名称,则可以使用$_GET
获取名称。
例如:
<form action="upload.php" method="post"> \\ this would send it to your page
<input type='text' size='30' name='filename'/>
<input type='submit' />
</form>
只需使用$_GET
这样的名称获取名称
$name = $_GET['filename'];
现在运行您的代码,但使用此名称移动文件
move_uploaded_file($_FILES['file']['tmp_name'][$i], $upload_directory . $name[$i])