Jump to content

Recommended Posts

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]
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.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).
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";
}
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]
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..
[!--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.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.
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..
[!--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]
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.
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*
[!--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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.