简体   繁体   English

如果已存在 php 代码,则自动重命名文件名

[英]auto rename of filename if already exist php code

upload.php上传.php

<form enctype="multipart/form-data" action="uploader.php" method="POST">
Choose a file: <input name="uploadedfile" type="file" /><br />
Choose a file1: <input name="uploadedfile1" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

在此处输入图片说明

image of the form upload.php表单upload.php的图片

uploader.php上传者.php

mysql_select_db("test");


$target_path = "uploads/" . basename( $_FILES['uploadedfile']['name']);
$currentfile = $_FILES['uploadedfile']['name']; 
$target_path1 = "upload1/" . basename( $_FILES['uploadedfile1']['name']);
$currentfile1 = basename( $_FILES['uploadedfile1']['name']); 


$dbfiles = mysql_query("SELECT * FROM new WHERE amount='$currentfile' || amount='$currentfile1'");
if(mysql_num_rows($dbfiles) > 0 )
{ 

//what code should i place here??    

}
else
{
        if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
        {
                echo "file1: ".$_FILES['uploadedfile']['name']."<br>";
                $file1 = basename( $_FILES['uploadedfile']['name']);
                mysql_query("insert into new (amount) values('$file1')");   
        }
        if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1))
        {
                echo "file2: ".basename( $_FILES['uploadedfile1']['name']);
                $file2 = basename( $_FILES['uploadedfile1']['name']);
                mysql_query("insert into new (amount) values('$file2')");   
        }
}

what code should i need to place inside of this if condition if(mysql_num_rows($dbfiles) > 0 ){ } so that if the filename of the file that was uploaded was already exist, the file should change its filename automatically for example there was abc.jpg existing, then some one upload a new file but the name and type is same as abc.jpg.. then the newly file must be, abc_1.jpg... and if some one upload as same as abc.jpg, the name should be abc_2.jpg.. abc_1.jpg must be the name that will save to the mysql, and abc_1.jpg must the file that will save to the prepared folder.. thx如果条件if(mysql_num_rows($dbfiles) > 0 ){ } ,如果上传的文件的文件名已经存在,该文件应该自动更改其文件名,例如有abc.jpg 存在,然后有人上传一个新文件,但名称和类型与 abc.jpg 相同.. 那么新文件必须是,abc_1.jpg... 如果有人上传与 abc.jpg 相同,名称应该是abc_2.jpg..abc_1.jpg必须是将保存到mysql的名称,abc_1.jpg必须是将保存到准备好的文件夹中的文件..thx

i get the idea and code here http://www.tizag.com/phpT/fileupload.php?MAX_FILE_SIZE=100000&uploadedfile=NeroMediaHub._63C8A7B0BBE5459F9AC436392B2FF50D.exe我在这里得到了想法和代码http://www.tizag.com/phpT/fileupload.php?MAX_FILE_SIZE=100000&uploadedfile=NeroMediaHub._63C8A7B0BBE5459F9AC436392B2FF50D.exe

try this尝试这个

<?php
$target_path = "uploads/" . basename( $_FILES['uploadedfile']['name']); 
$target_path1 = "uploads/" . basename( $_FILES['uploadedfile1']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
}
else{
    echo "There was an error uploading the file, please try again!";
}

if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1)) {
    echo "The file ".  basename( $_FILES['uploadedfile1']['name']). 
    " has been uploaded";
}
else{
    echo "There was an error uploading the file1, please try again!";
}
?>

Test with something like this...用这样的东西测试......

<?php
    $target_path = "uploads/" . basename( $_FILES['uploadedfile']['name']);
    $target_path1 = "uploads/" . basename( $_FILES['uploadedfile1']['name']);
    $file_saved = false;

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
    {
        echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
        $file_saved = true;
    }
    if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1))
    {
        echo "The file ".  basename( $_FILES['uploadedfile1']['name']). 
" has been uploaded";
        $file_saved = true;
    }
    if (!$file_saved)
    {
        echo "There was an error uploading the file, please try again!";
    }
?>

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

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