This is actually a cool subject, I think. A lot of different people do this their own way, some may make it more complicated than it needs to be and some people over simplify it.
I don't really know what my method is.. overly complicated or what not but this is how I name images.
When I upload an image, I look at what the pre-set variables are. Have a look-see
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
foreach( $_FILES as $file_name => $file_array )
{
echo "name: ".$file_array['name']."<br />\n";
echo "type: ".$file_array['type']."<br />\n";
echo "tmp_name: ".$file_array['tmp_name']."<br />\n";
echo "error: ".$file_array['error']."<br />\n";
echo "size: ".$file_array['size']."<br />\n";
// print_r($_FILES);
?>
If you notice the $file_array['tmp_name'] variable, it'll look something like
/tmp/PHPATYUIOPR354
When I first saw that, I thought... why not keep that temporary name? It's already named with a unique name in the /tmp/ directory.
Strip the location from the tmp_name. I.E remove "/tmp/" from /tmp/PHPATYUIOPR354
<?php
$tmp_name = $file_array['tmp_name'];
if( strstr( $tmp_name, "/tmp/" ) ) {
$tmp_name = str_replace("/tmp/", "", $tmp_name); # strip location from name
}
?>
That's just an example of how it could be done. I've got a script a lot like this already done.. i'll post it so you can play around with it and maybe get some good use from it.
<?php
# vars.php
/*
* Maximum Size an image may be
*
* $maxImageSize
*
* 51200 = 50 KB
* 512000 = 500 KB
*
*/
$maxImageSize = "512000";
/*
* Path Information
*
* 1.) $file_dir
*
* 2.)$file_url
*
* 1.) full path ex /home/user/public_html/images
*
* 2.) full url ex http://www.site.com/images
*
* NO TRAILING SLASH!!
* Directory to move images to should be chmod 777
*
*/
$file_dir = "/home/paul/public_html/FREE/upload/images";
$file_url = "http://localhost/~paul/FREE/upload/images";
/*
* Defining accepted Image Types
*
* Add more like shown below:
*
* $acceptedImageType[] = "type";
*
*/
$acceptedImageType = array ( "pjpeg", "pjpg", "jpg", "jpeg", "gif", "png", "x-png", "PNG", "x-xbitmap" );
$acceptedImageType[] = "x-bmp";
/*
* Defining a Page's Own URL
*
* $self=$_SERVER['PHP_SELF'];
*
*/
$self=$_SERVER['PHP_SELF'];
# upLoadForm.php
include("vars.php");
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
foreach( $_FILES as $file_name => $file_array )
{
echo "name: ".$file_array['name']."<br />\n";
echo "type: ".$file_array['type']."<br />\n";
echo "tmp_name: ".$file_array['tmp_name']."<br />\n";
echo "error: ".$file_array['error']."<br />\n";
echo "size: ".$file_array['size']."<br />\n";
// print_r($_FILES);
$tmp_name = $file_array['tmp_name'];
if( strstr( $tmp_name, "/tmp/" ) ) {
$tmp_name = str_replace("/tmp/", "", $tmp_name); # strip location from name
}
foreach ( $acceptedImageType as $acceptThis ) {
if(is_uploaded_file( $file_array['tmp_name'] ) && $file_array['type'] == "image/$acceptThis" ) {
$size=$file_array['size'];
if($size > $maxImageSize)
{
exit("The image is too large in size.");
}
move_uploaded_file( $file_array['tmp_name'], "$file_dir/".$tmp_name)
or die("Couldn't Copy");
echo "<img src=\"$file_url/".$tmp_name."\">";
echo "<br />\n\n";
} // End Foreach loop 2
} // End IF statement
} // End Foreach loop 1
?>
<form action="<?php echo "$self"; ?>" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo "$maxImageSize"; ?>">
<input type="file" name="fupload">
<br /><br />
<input type="submit" value="Upload">
<br />
</form>
Hope this helps!