doudula1974 2012-05-19 19:51
浏览 52
已采纳

PHP自动增加文件名

I have a file uploader and I want the filenames to auto increment number. I don't feel the need to use a database to do this and I want to keep the code relatively clean, I'm pretty new in file upload and management in PHP so I'm not exactly sure what to do. Could anyone direct me in the right path?

Here is my current code, it just uses an md5 of a bunch of seeds.

<?php
if(isset($_FILES['imagedata']['tmp_name']))
{
// Directory related to the location of your gyazo script
    $newName = 'images/' . substr(md5(rand() . time()), 0, 20) . '.png';
    $tf = fopen($newName, 'w');
    fclose($tf);
    move_uploaded_file($_FILES['imagedata']['tmp_name'], $newName);
// Website
    echo 'http://davidknag.com/' . $newName;
}
?>
  • 写回答

5条回答 默认 最新

  • doutang2017 2017-05-30 18:03
    关注
    function enc($length = "string") {
    if(!is_numeric($length) || $length > 255 || $length < 1){
    $length = rand("3","6");
    }
    
      //  $randomID = substr(uniqid(sha1(crypt(md5("".time("ysia", true)."".rand())))), 0, $length);
        $randomID = genUnique($length);
        $count = 0;
    while(glob("$randomID.*") || fetch("select * from `short` where `short` = '$randomID'") || fetch("select * from `images` where `name` = '$randomID'") || glob("img/$randomID.*") || is_numeric($randomID)){
            if($count > 20){
            $length++;
            }
        $randomID = genUnique($length);
        $count++;
        }
        return $randomID;
    }
    

    this code is pretty old (not even using mysqli), but i figured i'd include it first

    <?php
    include_once "functions.php";
    if(!isset($_REQUEST['api'])){
    notfound("");
    }
    $con = connect();
    $key = $_REQUEST['api'];
    $ver = $_REQUEST['version'];
    if($ver != "10-26-2016" || $key == "zoidberg")
    {
      die("Please upgrade your in4.us.exe by logging in and clicking download.");
    }
    if($key == "nokey"){
      die("You need to keep the exe with the ini file to pair your api key. Copy ini file to same directory or redownload.");
    }
    $key = mysql_real_escape_string($key);
    $findkey = fetch(" SELECT * from `users` where `key` = '$key' ");
    if(!is_array($findkey)){
    die("No user with that API Key found. Configure the INI File using your api key on in4.us");
    }
    $user = $findkey['username'];
        if(isset($_FILES['imagedata']['tmp_name'])){
            $newName =  enc();
        $tf = fopen("img/".$newName.".png", 'w');
        fclose($tf);
        move_uploaded_file($_FILES['imagedata']['tmp_name'], "img/".$newName.".png");
            $domain = $_SERVER['HTTP_HOST'];
        date_default_timezone_set('America/New_York');
        $mysqldate = date("Y-m-d H:i:s");
        $qry = mysql_query("INSERT INTO `images` (`name`, `added`, `dateadded`) VALUES ('$newName', '$user', '$mysqldate');");
        if(!qry){
              die('Invalid query: ' . mysql_error());
        }
        echo "http://$domain/$newName.png";
    
        disconnect($con);
    }else{
    notfound("");
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿