Jump to content

Archived

This topic is now archived and is closed to further replies.

RTS

Change Upload Name to Numbers

Recommended Posts

I have an upload script that uploads mp3's to a folder called music on my server, my question is, how do I make it so the first song uploaded is called 1.mp3, the seccond is called 2.mp3, and so on?

Share this post


Link to post
Share on other sites
You can use something like
[code=php:0]
for ($i = 0; $i < count($_FILES); $i++)
$_FILES['name'][$i] = $i . $_FILES['name'];
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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!

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.