Jump to content


Photo

Change Upload Name to Numbers


  • Please log in to reply
4 replies to this topic

#1 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 04 October 2006 - 03:15 AM

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?

#2 JayBachatero

JayBachatero
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationQueens NY

Posted 04 October 2006 - 03:55 AM

You can use something like
for ($i = 0; $i < count($_FILES); $i++)
	$_FILES['name'][$i] = $i . $_FILES['name'];

JayBachatero
SMF Developer && Converter Specialist

#3 RTS

RTS
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 04 October 2006 - 04:06 AM

hmmm, I dont quite get how I would do that with my code here it is by the way

<?php
    if (file_exists("music/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "music/instert number here.mp3");
      echo "Your song was uploaded successfully!";
      }
?>



#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 04 October 2006 - 04:06 AM

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

example...

<?

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;

?>


me!

#5 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 04 October 2006 - 04:25 AM

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

<?
$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.";
      } 
?>

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.
Why doesn't anyone ever say hi, hey, or whad up world?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users