我在网站上有一个上传表单,可以将多个文件上传到服务器,还会向我发送一封电子邮件。 它是用php编写的,主文件部分如下:
if (isset($_FILES) && (bool) $_FILES) {
$files = array();
$ext_error = "";
// Define allowed extensions
// blahblahblah checking
// Store attached files in uploads folder
$server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];
move_uploaded_file($temp_name, $server_file);
array_push($files, $server_file);
如今,人们通过手机上传图片,并且通常它们都被命名为相同的文件名:image.jpg
(或类似的东西) - 所以它们会被覆盖。
我想在每个多个文件(如1,2,3 ......)上附加一个计数器,以便上传并以唯一名称发送,即使客户端将它们作为同名发送。
类似的东西:
if (isset($_FILES) && (bool) $_FILES) {
$files = array();
$ext_error = "";
// Define allowed extensions
// counter= counter++;
// newFilename=oldFileName+String(counter);
// doRestStuffNewFileName();
// blahblahblah checking
// Store attached files in uploads folder
$server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];`
`move_uploaded_file($temp_name, $server_file);`
array_push($files, $server_file);`
如何在php中修改它?
好的新评论:
我想:
for int i=0;i<files[attached];i++;
fileName=files[i]
newFileName=filename+String(Integer(i));
uploadWithNewFileName();
writeToServerWithNewFileName();
这是php当前代码:
if (isset($_FILES) && (bool) $_FILES) {
$files = array();
$ext_error = "";
foreach ($_FILES as $name => $file) {
if (!$file['name'] == "") {
$file_name = $file['name'];
$size += $file['size'];
$temp_name = $file['tmp_name'];
$path_part = pathinfo($file_name);
$ext = $path_part['extension'];
// Store attached files in uploads folder
$server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];
move_uploaded_file($temp_name, $server_file);
array_push($files, $server_file);
}
}
为什么这不起作用:
if (isset($_FILES) && (bool) $_FILES) {
$files = array();
$ext_error = "";
counter =0; //My code
foreach ($_FILES as $name => $file) {
counter++; //My code
if (!$file['name'] == "") {
$file_name = $file['name'];
$size += $file['size'];
$temp_name = $file['tmp_name'];
$path_part = pathinfo($file_name) + counter; //My code
$ext = $path_part['extension'];
// Store attached files in uploads folder
$server_file = dirname(__FILE__) . "/uploads/" . $path_part['basename'];
move_uploaded_file($temp_name, $server_file);
array_push($files, $server_file);
}
}