corillo181 Posted May 20, 2006 Share Posted May 20, 2006 $filename = $_FILES['ulfile']['name'];$random=rand(0000,9999);$newname=$random.$filename;how do i change this so the file is only numbers with out the name.. tried a few ways doesn't wok.. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/ Share on other sites More sharing options...
zq29 Posted May 20, 2006 Share Posted May 20, 2006 This [i]should[/i] work...[code]<?php$filename = $_FILES['ulfile']['name'];$random=rand(0000,9999);$pos = strrpos($filename,".");$mpos = "-".$pos; //Not sure if substr would work with -$pos as a parameter, so created this var.$newname=$random.substr($filename,$mpos,$pos);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37432 Share on other sites More sharing options...
corillo181 Posted May 20, 2006 Author Share Posted May 20, 2006 that code doesn't do nothing at alll.. just took half the name away from the file and nothing else.. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37442 Share on other sites More sharing options...
toplay Posted May 20, 2006 Share Posted May 20, 2006 That code works. What are you wanting exactly?SemiApocalyptic code works:[code]$filename = 'test.txt'; // $_FILES['ulfile']['name'];$random = rand(0,9999);$pos = strrpos($filename, '.');$newname = $random . substr($filename, $pos * -1, $pos);echo $newname;[/code]Would output something like: 123.txtHis code is attempting to get the extension for you and include it at the end of the filename (after the random number is picked). If the filename was just "test", then you would just get something like: 123Your code shows: $random=rand(0000,9999);Putting four zeros is pointless. If you want to always have a four digit number, then you have to set the minimum random value to start with one thousand:$random = rand(1000, 9999); It's best to start a filename with a letter.SemiApocalyptic, just multiply by -1 to turn a positive number to a negative (and vice versa) instead of concatenating a dash (minus sign). Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37452 Share on other sites More sharing options...
corillo181 Posted May 20, 2006 Author Share Posted May 20, 2006 still it doesn't workthis is how i got my upload fomr set up..$filename = $_FILES['ulfile']['name'];$random=rand(1000,9999);$pos = strrpos($filename, '.');$newname = $random . substr($filename, $pos * -1, $pos);$tmpname = $_FILES['ulfile']['tmp_name'];$filesize = $_FILES['ulfile']['size'];$filetype = $_FILES['ulfile']['type'];$descrive = $_POST['desrive'];$datetime = date('m-d-y h:i:s');$descrive = $_POST['descrive'];$filepath = $dir . $newname;$result=copy($tmpname, $filepath);if(!$result){ echo " a problem occur";} Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37461 Share on other sites More sharing options...
toplay Posted May 20, 2006 Share Posted May 20, 2006 What doesn't work, the upload itself? The copy or what?When posting be as specific as you can and list any errors. You didn't post your HTML form.Read this topic and that should solve most of the problems or questions you may have:[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=93591\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?showtopic=93591[/a] Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37465 Share on other sites More sharing options...
corillo181 Posted May 20, 2006 Author Share Posted May 20, 2006 everything with my script works.. i simply want to change the whole name of the file..i dont want to use the original nama of the file that the user has.. i want to change it to a rand number or what ever..i tried using that code that they guy gave me and it does not replace the name it just add numbers to it.. i want to replace the whole number.. change it to rand numbers.. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37473 Share on other sites More sharing options...
toplay Posted May 20, 2006 Share Posted May 20, 2006 [!--quoteo(post=375548:date=May 20 2006, 12:18 PM:name=Richard181)--][div class=\'quotetop\']QUOTE(Richard181 @ May 20 2006, 12:18 PM) [snapback]375548[/snapback][/div][div class=\'quotemain\'][!--quotec--]everything with my script works.. i simply want to change the whole name of the file..i dont want to use the original nama of the file that the user has.. i want to change it to a rand number or what ever..i tried using that code that they guy gave me and it does not replace the name it just add numbers to it.. i want to replace the whole number.. change it to rand numbers..[/quote]You're not making much sense to me. What are you looking at or displaying to make you say that isn't not working. Are you displaying the temporary filename perhaps? Because that remains in your designated temporary upload directory until you delete it. That name remains what the user specified and doesn't get changed (unless you do it). The destination filename is what we've been trying to help you with.If you just run the code I last posted just as is, you'll see that you get a filename with random numbers and an extension. If you don't want an extension, then just use $random as the filename and don't bother with the the strrpos() and substr() functions.Are you not seeing it pick random numbers? If you're using a PHP version less than 4.2.0 then, you must use srand() before rand():[a href=\"http://us2.php.net/manual/en/function.srand.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.srand.php[/a]I don't understand what the problem is exactly or what you want now. So, indicate what the name of the file you're uploading is called, then indicate exactly how you want it to be. Because as far as I'm concerned, we have already given you the answer.In your code, I don't see where you set $dir but I assume you have somewhere else in the code that we haven't seen yet.Your code doesn't check for errors, which is a good thing to do. If you read the topic link provided you will read about that and other functions and tips to use.From the code we posted, here are examples of input (uploaded) filenames and (--->) their output examples:a_nice_filename.txt ---> 1234.txtresume.doc ---> 6843.doc1234.jpg ---> 9484.jpg3434 ---> 1844A.Great.Novel.pdf ---> 6493.pdfa-file ---> 1933Help911.xls ---> 3783.xlsetc., etc., etc. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37478 Share on other sites More sharing options...
corillo181 Posted May 20, 2006 Author Share Posted May 20, 2006 yes that is what i'm trying to do.. not the extension just the name ..like someone up load a picture..mypicture.jpgi want to change the nameto 34567.jpgrand numbers..and the code as i said before they do work..the only problem is that it adds the rand number to the name..like 34567mypicture.jpgnow that is not what i want becuase if the user uploads a file with a name that has space in it you know is not going to displaymy picture.jpgso that is what i'm trying to avoid..as i said.. the script works fine i'm using the new php..the only thing wrong is that it add the rand number to the current name..i want the name to change just to the rand numbers.. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37487 Share on other sites More sharing options...
zq29 Posted May 20, 2006 Share Posted May 20, 2006 [!--quoteo(post=375527:date=May 20 2006, 05:28 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 20 2006, 05:28 PM) [snapback]375527[/snapback][/div][div class=\'quotemain\'][!--quotec--]Your code shows: $random=rand(0000,9999);Putting four zeros is pointless. If you want to always have a four digit number, then you have to set the minimum random value to start with one thousand:$random = rand(1000, 9999);[/quote]An option could be to use str_pad(rand(0,9999),4,"0",STR_PAD_LEFT); should you wish to have four digits starting from 0000 to 9999.[!--quoteo(post=375527:date=May 20 2006, 05:28 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 20 2006, 05:28 PM) [snapback]375527[/snapback][/div][div class=\'quotemain\'][!--quotec--]SemiApocalyptic, just multiply by -1 to turn a positive number to a negative (and vice versa) instead of concatenating a dash (minus sign).[/quote] Thanks! Learn something new every day!With regards to the main question, I can't see why my original code doesn't work, along with Toplay's revision. I'll post it up again with comments to show whats going on:[code]<?php$filename = $_FILES['ulfile']['name']; //Lets call it test.txt$random = str_pad(rand(0,9999),4,"0",STR_PAD_LEFT); //Pick a number between 0000 and 9999$pos = strrpos($filename,"."); //Get the location of the last period we can extract the extension$newname = $random.substr($filename,$pos * -1,$pos); //Add the random number to the extension .txt?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37490 Share on other sites More sharing options...
corillo181 Posted May 20, 2006 Author Share Posted May 20, 2006 ohi know the problem.. since rand is set to 4 the file onle change the 4 first letters of the file..so i tihnk i can work a change len in there to then change the whole file name.. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37496 Share on other sites More sharing options...
toplay Posted May 20, 2006 Share Posted May 20, 2006 Yeah, if you need the numbers to start with zero, then you can use SemiApocalyptic str_pad() example.There's many ways to achieve the same result. You can use explode() or preg_replace(). Here's another way of doing it:[code]$filename = 'A_Great_Novel.doc'; // $_FILES['ulfile']['name'];$random = str_pad(rand(0,9999), 4, '0', STR_PAD_LEFT);$file_ext = pathinfo($filename, PATHINFO_EXTENSION);$newname = (!empty($file_ext)) ? $random . '.' . $file_ext : $random;echo $newname; // Displays something like 0358.doc[/code]Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37497 Share on other sites More sharing options...
zq29 Posted May 20, 2006 Share Posted May 20, 2006 Just ran a few tests, and finaly managed to get it to [i]not[/i] work for me. Think I made a silly mistake in my code... Try replacing this line:[code]$newname = $random.substr($filename,$pos);[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37500 Share on other sites More sharing options...
corillo181 Posted May 20, 2006 Author Share Posted May 20, 2006 as a metter of fact i was messin around with the code you posted and it worked fine just had to get rid of the *-1after i took that off everything worked fine..now it changes the name unto random numbersedit *funny how i just found tha tout too nice timing :D* Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37501 Share on other sites More sharing options...
toplay Posted May 20, 2006 Share Posted May 20, 2006 [!--quoteo(post=375577:date=May 20 2006, 02:08 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ May 20 2006, 02:08 PM) [snapback]375577[/snapback][/div][div class=\'quotemain\'][!--quotec--]Just ran a few tests, and finaly managed to get it to [i]not[/i] work for me. Think I made a silly mistake in my code... Try replacing this line:[code]$newname = $random.substr($filename,$pos);[/code][/quote]The strrpos() and substr() approach is fine as long as a filename doesn't end with a period. i.e. a_test. which will become something like 0123. which may or may not be what Richard181 wants. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37506 Share on other sites More sharing options...
corillo181 Posted May 20, 2006 Author Share Posted May 20, 2006 but thanks to you i can now move forwar don my training.. thanx man.. Quote Link to comment https://forums.phpfreaks.com/topic/10064-solved-upload-file-rename/#findComment-37507 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.