Jump to content


Photo

*SOLVED* upload file rename


  • Please log in to reply
15 replies to this topic

#1 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 02:32 PM

$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..

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 May 2006 - 03:02 PM

This should work...
<?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);
?>


#3 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 03:39 PM

that code doesn't do nothing at alll.. just took half the name away from the file and nothing else..

#4 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 20 May 2006 - 04:28 PM

That code works. What are you wanting exactly?

SemiApocalyptic code works:
$filename = 'test.txt';  // $_FILES['ulfile']['name'];
$random = rand(0,9999);
$pos = strrpos($filename, '.');
$newname = $random . substr($filename, $pos * -1, $pos);
echo $newname;
Would output something like: 123.txt

His 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: 123

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);


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).

#5 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 05:58 PM

still it doesn't work

this 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";
}


#6 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 20 May 2006 - 06:47 PM

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]

#7 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 07:18 PM

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..

#8 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 20 May 2006 - 07:46 PM

[!--quoteo(post=375548:date=May 20 2006, 12:18 PM:name=Richard181)--][div class=\'quotetop\']QUOTE(Richard181 @ May 20 2006, 12:18 PM) View Post[/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.txt
resume.doc ---> 6843.doc
1234.jpg ---> 9484.jpg
3434 ---> 1844
A.Great.Novel.pdf ---> 6493.pdf
a-file ---> 1933
Help911.xls ---> 3783.xls

etc., etc., etc.

#9 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 08:21 PM

yes that is what i'm trying to do.. not the extension just the name ..

like someone up load a picture..

mypicture.jpg

i want to change the name

to

34567.jpg

rand 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.jpg

now 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 display

my picture.jpg

so 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..

#10 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 May 2006 - 08:30 PM

[!--quoteo(post=375527:date=May 20 2006, 05:28 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 20 2006, 05:28 PM) View Post[/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) View Post[/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:
<?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
?>


#11 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 08:53 PM

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..

#12 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 20 May 2006 - 09:01 PM

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:
$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


Good luck.

#13 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 May 2006 - 09:08 PM

Just ran a few tests, and finaly managed to get it to not work for me. Think I made a silly mistake in my code... Try replacing this line:
$newname = $random.substr($filename,$pos);


#14 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 09:13 PM

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 *-1

after i took that off everything worked fine..

now it changes the name unto random numbers


edit *funny how i just found tha tout too nice timing :D*

#15 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 20 May 2006 - 09:16 PM

[!--quoteo(post=375577:date=May 20 2006, 02:08 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ May 20 2006, 02:08 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Just ran a few tests, and finaly managed to get it to not work for me. Think I made a silly mistake in my code... Try replacing this line:
$newname = $random.substr($filename,$pos);
[/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.


#16 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 20 May 2006 - 09:16 PM

but thanks to you i can now move forwar don my training..

thanx man..




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users