找到重复的文件名时,重命名并在目录中存储文件

时间:2018-01-21 17:49:44

标签: php

如果在目录中找到重复的文件名,我想在文件名末尾或数据库中上传数字。就像我上传imag.jpg一样,它应该是一些uniqueid + imag.php。我研究了多种资源,但现在几乎感到困惑。 这是w3schools的默认参考代码。因为我是新人,并从那里和实践中学习,所以,任何帮助都会感谢你。

    if($_POST){

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
        $pptimg =  $_FILES["file"]["name"];
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

}

2 个答案:

答案 0 :(得分:0)

使用包含秒的时间戳保存文件。

具有相同文件名的另一个文件的可能性很低。

答案 1 :(得分:0)

您可以使用uniqid()php函数来执行此操作。

bcp "SELECT * FROM DatabaseLog" queryout "C:\Test\TestingBulkCopy.txt" -t\t -c -T