English 中文(简体)
Generate unique names?
原标题:
  • 时间:2009-11-15 18:09:22
  •  标签:
  • php
  • random

I am working on a php site in which we have to upload images from users.i have to rename that file for preventing conflicts in the name of the image.

uniqid(rand(), true);
and adding a large random number after it.

Will this work perfectly. Any suggestions..??

Its about generation unique names for the image.....

最佳回答

Function tempnam() creates a file with a unique name.

问题回答

Take an md5 of the file and use that. IIRC, the odds of a collision are 1 in 64M. If that s not enough, prefix it with the timestamp expressed in seconds or milliseconds. That way even if a duplicate md5 is generated, the files would have to come in during the same second/millisecond for a collision.

You can use Base36 on the AutoIncrement value from a SQL Table (hoping that you do use a SQL table).

$filename = base_convert($last_insert_id, 10, 36);

You have two approaches depending on "how" big can be your image library: 1. for a non-big amount of files I do this

<?php

$file = sanitize_file($file); // remove all no [az-09_] characters for safe url linking;
$file_md5 = $md5($file);
$file_extention = $md5($file);

// since I assume the file should belongs to someone you can do this
$file_name = $user_id . $file_md5 . $file_extension;

// then save the file

?>
  1. option.... CacheMogul. Here you need to use your imagination. but for huge amount of files this does a nice sharding so you dont need to worry about a folder max quantity or size




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签