如何更改上载的文件名以使序列号名称不随机

时间:2012-10-31 21:38:16

标签: php file upload

我的网站上有一个上传器,它最后用一个随机数重命名图像文件(“原始名称”+“随机数”)。

我希望上传的文件按顺序排列,因此最近上传的图片必须位于自动填充页面的顶部。

现在代码有随机数生成器,然后它取出原始文件名并将其放在开头..

我只想让所有图片都像10.jpg那样阅读下一张上传的照片是9.jpg和8.jpg

自动填充功能我现在已将最小的数字放在页面上

如何做到这一点。我担心的是,当下一个人上传时,它将会超过之前的“起始”点10.jpg文件?

您可以在此处查看php文件http://ilovesmallies.com/forum/processupload.php

 // Random number for both file, will be added after image name
    $RandomNumber   = rand(0, 9999999999); 


//Get file extension from Image name, this will be re-added after random name
    $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
    $ImageExt = str_replace('.','',$ImageExt);

编辑:所有归档都上传到目录文件夹。 我在单独的页面上使用此代码将它们绘制成div。然后自动填充该页面上的缩略图。

<script>
$.ajax({
  url: "user-uploads-thumbnails",
  success: function(data){
     $(data).find("a:contains(.jpg)").each(function(){
        // will loop through 
var images = 'user-uploads-thumbnails/' + $(this).attr("href");
var linkimage = 'user-uploads/' + $(this).attr("href");
 //backup $('<p><a href="' + linkimage + '"><img src="' + images + '"></a></p>').appendTo('#content');
$('<p><a class="fancybox" href="' + linkimage + '" data-fancybox-group="gallery"><img src="' + images + '"></a></p>').appendTo('#content');
     });
  }
});

1 个答案:

答案 0 :(得分:0)

这是一种非常简单的方法。如果没有两个人在同一时间上传....

您需要创建一个包含以下内容的目录:

  • index.php
  • anotherpage.php //这个名字并不重要(这只是为了抓住最新的图片
  • / upload
  • count.txt
  • inc.php

的index.php

<?php


    include("inc.php");
    if(isset($_FILES["file"])){
        $new_filename = read_file($counter_file); // fetch the number for filename
        $new_filename++; // increment the counter
        move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $new_filename.".jpg"); // assumes jpg (easily extended to other formats)
        write_to_file($new_filename,$counter_file);

        echo "<img src=\"upload/".$new_filename.".jpg\" /><br />";
    }

?>

<form id="upload" action="index.php" enctype="multipart/form-data" method="post"> 
    <input type="file" name="file" id="file" /> <br />
    <input type="submit" value="go" />  
</form>

anotherpage.php

<?php

    include("inc.php");

    $new_filename = read_file($counter_file);
    echo "<img src=\"upload/".$new_filename.".jpg\" /><br />";

?>

inc.php

<?php

        $counter_file = "count.txt"; // name of the file containing your file count

        function write_to_file($text,$filename){
            $fp = fopen($filename, 'w');
            fwrite($fp, $text);
            fclose($fp);
        }

        function read_file($filename){
        $handle = fopen($filename, "r");
        $contents = fread($handle, filesize($filename));
        fclose($handle);
        return($contents);
        }

?>

警告:这包含NO VALIDATION。请记住,文件的mime类型可以被删除。这是解决您问题的简单方法。