Jump to content

Change Upload Name to Numbers


RTS

Recommended Posts

hmmm, I dont quite get how I would do that with my code here it is by the way
[quote]<?php
    if (file_exists("music/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "music/[color=red]instert number here[/color].mp3");
      echo "Your song was uploaded successfully!";
      }
?>[/quote]
Link to comment
Share on other sites

create a placeholder file and read the next number from that, it much better than reading the complete directory for the next number!

example...

[code=php:0]
<?

function get_next ( $file )
{
if ( ! file_exists ( $file ) )
{
$io = fopen ( $file, 'w' );
fputs ( $io, sprintf ( '%8s', 2 ) );
fclose ( $io );
return ( 1 );
}
else
{
$io = fopen ( $file, 'r+' );
$out = trim ( fread ( $io, 8 ) );
rewind ( $io );
fputs ( $io, sprintf ( '%8s', ( $out + 1 ) ) );
fclose ( $io );
return ( $out );
}
}

// place holder file

$path = './mp3/my_counter.dat';

// return the next number

$next_mp3 = get_next ( $path );

// just show the example result!

echo $next_mp3;

?>
[/code]


me!
Link to comment
Share on other sites

The code posted by printf will always work and it will have nice corrosponding numbers, but sometimes random can be good :P.

[code=php:0]
<?
$str = "a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|w|z|0|1|2|3|4|5|6|7|8|9";
$str_a = explode("|", $str);
$fn = explode(".", $_FILES["file"]["name"]);
$c = count($fn);
if($fn[$c - 1] != "mpg") {
die("File must be .mpg file extension!");
}

    while(file_exists("music/" . $rand . ".mp3"))
    {
$rand = NULL;
while($i < 8) {
$rand .= $str_a[rand(0,34)];
}
    }

if(!file_exists("music/" . $rand . ".mpg") {
if(move_uploaded_file($_FILES["file"]["tmp_name"], $rand . ".mp3")) {
?>
Your file <i><?=$_FILES["file"]["name"];?></i> has been successfully uploaded to <a href="music/<?=$rand;?>.mp3"></a>.

<?
}
else {
echo "Sorry, your file could not be uploaded.";
}
}
    else
      {
      echo "Unable to upload.  Please try again.";
      }
?>
[/code]

Will randomly name it something like music/c8e03l78.mpg, and it wont rely on a file.  If the file exists it will simply generate another name.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.