RTS Posted October 4, 2006 Share Posted October 4, 2006 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? Link to comment https://forums.phpfreaks.com/topic/22943-change-upload-name-to-numbers/ Share on other sites More sharing options...
JayBachatero Posted October 4, 2006 Share Posted October 4, 2006 You can use something like[code=php:0]for ($i = 0; $i < count($_FILES); $i++) $_FILES['name'][$i] = $i . $_FILES['name'];[/code] Link to comment https://forums.phpfreaks.com/topic/22943-change-upload-name-to-numbers/#findComment-103536 Share on other sites More sharing options...
RTS Posted October 4, 2006 Author Share Posted October 4, 2006 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 https://forums.phpfreaks.com/topic/22943-change-upload-name-to-numbers/#findComment-103540 Share on other sites More sharing options...
printf Posted October 4, 2006 Share Posted October 4, 2006 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 https://forums.phpfreaks.com/topic/22943-change-upload-name-to-numbers/#findComment-103541 Share on other sites More sharing options...
corbin Posted October 4, 2006 Share Posted October 4, 2006 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 https://forums.phpfreaks.com/topic/22943-change-upload-name-to-numbers/#findComment-103550 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.