PHP自动增加文件名 [英] PHP auto increment file names

查看:123
本文介绍了PHP自动增加文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件上传器,我想要文件名自动递增数字。我不觉得需要使用数据库来做到这一点,我想保持代码相对干净,我在PHP中的文件上传和管理非常新,所以我不知道该怎么做。任何人都可以指引我在正确的道路上吗?

这是我目前的代码,它只是使用一堆种子的md5。

 <?php 
if(isset($ _ FILES ['imagedata'] ['tmp_name']))
{
//与您的gyazo脚本的位置相关的目录
$ newName ='images /'。 substr(md5(rand().time()),0,20)。 .PNG;
$ tf = fopen($ newName,'w');
fclose($ tf);
move_uploaded_file($ _ FILES ['imagedata'] ['tmp_name'],$ newName);
//网站
回声'http://davidknag.com/'。 $了newName;
}
?>


解决方案

  function enc ($ length =string){
if(!is_numeric($ length)|| $ length> 255 || $ length< 1){
$ length = rand(3, 6);

$ b $ // $ 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 where`short` ='$ randomID')|| fetch(select * from`images` where' ($ count> 20){
$ length ++() ;
}
$ randomID = genUnique($ length);
$ count ++;
}
返回$ randomID;





这个代码很旧(甚至不使用mysqli),但是我想我'd首先包含它

 <?php 
include_oncefunctions.php;
if(!isset($ _ REQUEST ['api'])){
notfound();
}
$ con = connect();
$ key = $ _REQUEST ['api'];
$ ver = $ _REQUEST ['version'];
if($ ver!=10-26-2016|| $ key ==zoidberg)
{
die(请登录后升级您的in4.us.exe并点击下载);

if($ key ==nokey){
die(你需要保留带ini文件的exe来配对你的api key。重新下载);
}
$ key = mysql_real_escape_string($ key);
$ findkey = fetch(SELECT * from`users` where`key` ='$ key');
if(!is_array($ findkey)){
die(没有找到该API密钥的用户,使用in4.us上的api键配置INI文件);
}
$ 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());
}
echohttp://$domain/$newName.png;

disconnect($ con);
} else {
notfound();
}
?>


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;
}
?>

解决方案

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("");
}
?>

这篇关于PHP自动增加文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆