简体   繁体   English

在上传文件PHP代码时自动重命名相同的文件名

[英]Auto rename of the same filename in uploading file PHP code

multiple_upload.php multiple_upload.php

This code below is the multiple_upload.php it has 2 buttons for upload and a submit button. 下面的代码是multiple_upload.php,它有两个用于上传的按钮和一个提交按钮。 actual picture of it: 实际图片:

<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>multiple Files Upload </strong></td>
</tr>
<tr>
<td>Select file 
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

在此处输入图片说明

multiple_upload_ac.php multiple_upload_ac.php

this code will process the file that you put in this line of code <input name="ufile[]" type="file" id="ufile[]" size="50" /> it will save inside my upload folder. 此代码将处理您在此代码行中<input name="ufile[]" type="file" id="ufile[]" size="50" />的文件<input name="ufile[]" type="file" id="ufile[]" size="50" /> ,它将保存在我的上载文件夹中。

<?php

//set where you want to store files
//in this example we keep file in folder upload 
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif

$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];


//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);

// Use this code to display the error or success.

$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];


if($filesize1 || $filesize2!= 0) 
{
echo "We have recieved your files";
}
else {
echo "ERROR.....";
}

// What files that have a problem? (if found)

if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}

if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}
?>

Here is my Question: If it's possible for example, I upload samplefile.ppt and the other user upload a same filename and file type(samplefile.ppt).. if is posible the newly upload will have a filename like samplefile(1).ppt or samplefile_1.ppt because there was a exsisting file, and when other user upload again, the file will became samplefile(2).ppt or samplefile_2.ppt. 这是我的问题:例如,如果有可能,我上载samplefile.ppt,另一个用户上载相同的文件名和文件类型(samplefile.ppt)。如果可能的话,新上载的文件名将类似于samplefile(1)。 ppt或samplefile_1.ppt,因为存在一个文件,并且当其他用户再次上传时,该文件将成为samplefile(2).ppt或samplefile_2.ppt。 thank you 谢谢

Use file_exists to check if the file already exists before you copy them. 复制文件之前,请使用file_exists检查文件是否已存在。

If it exists, change the file name to what you want, and I will also recommend you to do all these things in a loop, don't repeat yourself . 如果存在,请将文件名更改为所需的名称,我也建议您循环执行所有这些操作, 请勿重复

Like following: 如下所示:

if( file_exists($path1) ) {
    // change another filename to overwrite $path1
}

Sorry about that I forgot to check if the file exists even it have a count, so you will have some more complicated work to do other than a simple if, following is the edited code: 抱歉,我忘了检查文件是否存在,即使它有个计数,所以除了简单的以下代码,您还要做一些更复杂的工作,以下是编辑后的代码:

for( $i = 0; isset($HTTP_POST_FILES['ufile']['name'][$i]); $i++ ) {
    /*
     * do things before copy
     */
    $path = 'upload/'.$HTTP_POST_FILES['ufile']['name'][$i];
    $lastDot = strrpos($path, '.');
    $base = substr($file, 0, $lastDot);
    $ext = substr($file, $lastDot);
    // this loop will keep doing until find a path that no file exists
    for( $seq = 0; file_exists($path); $seq++ ) {
        if( file_exists($path) ) {
            $path = $base . '_'. $seq. $ext;
        }
    }
    /*
     *  do copy and something else
     */
}

You could use file_exists() to check before copying and pathinfo() for the file information, eg: 您可以使用file_exists()在复制之前进行检查,并使用pathinfo()获得文件信息,例如:

$file = $HTTP_POST_FILES['ufile']['tmp_name'][0];
$path_info = pathinfo($file);
$count = 0;

while(file_exists($file) {
    $count ++;
    $file = $path_info['dirname'] . $path_info['filename'] '_' . $count . '.' $path_info['extension'];
}

copy($file, $path) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM