duanbi3385 2012-10-30 08:28 采纳率: 100%
浏览 178

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

multiple_upload.php

This code below is the multiple_upload.php it has 2 buttons for upload and a submit button. 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>

enter image description here

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.

<?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. thank you

  • 写回答

2条回答 默认 最新

  • dongtu1357 2012-10-30 08:38
    关注

    Use file_exists to check if the file already exists before you copy them.

    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
         */
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?