/    Sign up×
Community /Pin to ProfileBookmark

Rename an uploaded file

Hi, i am using this code to currently upload a file
but how can i rename the item, with keeping the extension the same.

[code=php]
<?PHP
$file_dir = “examples/”;

foreach($_FILES as $file_name => $file_array) {
echo “Path: “.$file_array[‘tmp_name’].”<br>n”;
echo “Name: “.$file_array[‘name’].”<br>n”;
echo “Type: “.$file_array[‘type’].”<br>n”;
echo “Size: “.$file_array[‘size’].”<br>n”;

if (is_uploaded_file($file_array[‘tmp_name’])) {
move_uploaded_file($file_array[‘tmp_name’],
“$file_dir/$file_array[name]”) or die (“Couldn’t copy”);
echo “file was moved!<br><br>”;
}
}
?>
[/code]

Many Thanks

Adam

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@Paul_JrAug 26.2004 — I can't say for sure, but couldn't you rename it in the [font=courier]move_uploaded_file()[/font] function?
[code=php]
$file = $_FILE['upload']['tmp_name'];
$ext = explode('/', mime_content_type($file));
move_uploaded_file($file, 'images/uploaded/' . $new_name . $ext[1]);
[/code]
Copy linkTweet thisAlerts:
@k0r54authorAug 26.2004 — Hi, i am using this code in the function but im i dont think its quite right.

I am gettin this error

Fatal error: Call to undefined function: mime_content_type() in /home/sites/apc-compunet.co.uk/public_html/test/php/upload/upload.php on line 21


[code=php]
if (is_uploaded_file($file_array['tmp_name'])) {
$file = $_FILE['upload']['tmp_name'];
$ext = explode('/', mime_content_type($file));
move_uploaded_file($file, 'example/' . $new . $ext[1]);

echo "file was moved!<br><br>";
}
}

Many Thanks Adam
[/code]
Copy linkTweet thisAlerts:
@solavarAug 26.2004 — When you got the error, you should have checked the manual.

The manual says:

(PHP 4 >= 4.3.0, PHP 5)

Which means

mime_content_type() is available only in versions greater than version 4.3.0

Then you could have checked what version you have. Is it greater or less?

That way, you could have seen why you were getting the error!
Copy linkTweet thisAlerts:
@k0r54authorAug 26.2004 — Hi, there PHP version is 4.3.8 but it still isn't working

any idea's

thanks adam
Copy linkTweet thisAlerts:
@Paul_JrAug 26.2004 — Your host may not have that extension installed. I run my own server, so I can install extensions as I need/want them. There are other ways to get the extension, but I think that's the most reliable. Alternatively, you could try:
[code=php]
$ext = explode('.', $file);
[/code]
Copy linkTweet thisAlerts:
@k0r54authorAug 27.2004 — Sorry i have changes loadz of things, but how exacly does it need to be coded

Many thanks adam
Copy linkTweet thisAlerts:
@k0r54authorAug 27.2004 — hi also im a bit confussed on where i put the new name?

This is what i have so far and the error is

Parse error: parse error, unexpected T_STRING on line 23


[code=php]
<?PHP
$file_dir = "examples/";

foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";

if (is_uploaded_file($file_array['tmp_name'])) {
$file = $_FILE['upload']['tmp_name'];
$ext = explode('.', $file);
$new = newname //is this where the new name goes
move_uploaded_file($file, 'example/' . $new . $ext);

echo "file was moved!<br><br>";
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@solavarAug 27.2004 — I take it you are trying to get the extension of a file.

OK

First of all, we know that a filename has a period, then the extension.

Something like:

"blahblah.txt"

But we know also, that the filename can actually have more than one period.

Something like:

"blahblahblah.some.thing.txt"

What we want to do is this:

Regardless of the number of periods in the filename, we must extract the characters after the LAST period. Those characters are the EXTENSION.

Now that we have defined what we want to do, coding it is easy.

[code=php]

//$filename = "blahblahblah.some.thing.txt";

//step 1: Split the filename into bits, using the period as the delimiter (or separator) and place those bits into an array

$nameparts = explode(".", $filename);

/* In this example, the array, $nameparts, now has the following:
$nameparts[0] has 'blahblahblah'
$nameparts[1] has 'some'
$nameparts[2] has 'thing'
$nameparts[3] has 'txt'
*/

// Question: How do we tell it that we want the last element, $nameparts[3] ?

// step 2: Get the number of elements:
$num_elements = sizeof($nameparts);

// you can also say: $num_elements = count($nameparts)...PHP is rich with alternative ways of doing things.

/* In this example, you can see that the number of elements will be four. You can also see, very clearly, that the last element, the fourth one, has an index of 3. It has to have an index which is ONE less than the maximum because the array index starts from zero.

In other words, to get the file extension, we need to give the array an index of ONE LESS THAN the MAXIMUM number of elements.
*/

// Step 3: Index of Last element:
$index_of_last = $num_elements - 1;

// Step 4: Get the Extension
$extension = $nameparts[$index_of_last];

[/code]


Echo $extension will give you 'txt'
Copy linkTweet thisAlerts:
@k0r54authorAug 27.2004 — Hi, im now gettin know error but its not displaying the ext value and its not uploading the file? but the other echo's are displayed as if it is

This is the current code
[code=php]
<?PHP
$file_dir = "examples/";

//This breaks it up into its seperate parts
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";

//if the file is allowed
if (is_uploaded_file($file_array['tmp_name'])) {
$file = $_FILE['upload']['tmp_name'];

//breaks the extensions up into parts
$nameparts = explode(".", $file);

//find the number of extensions
$num_elements = sizeof($nameparts);

//this locates the last element by find the total and takin away one
$index_of_last = $num_elements - 1;

//this is the new extension
$ext = $nameparts[$index_of_last];

//the new name
$new = newname;

move_uploaded_file($file, 'examples/' . $new . $ext);

echo "file was moved! $ext <br><br>";
}
}
?>
[/code]


P.S Thanks every1 for this im sure im becomin hassle! :o
Copy linkTweet thisAlerts:
@solavarAug 27.2004 — I can't see what this part is referring to...

[code=php]

//the new name
$new = newname;

[/code]


What is newname?

PS: You are not being a hassle.

Keep up the good work. You are really making an effort.
Copy linkTweet thisAlerts:
@k0r54authorAug 27.2004 — lol thanks alot.

the $new is refering to

[code=php]
ove_uploaded_file($file, 'examples/' . $new . $ext);
[/code]


i figured that the $new had nothing to look at and i couldn't c how i named the file so it is what i want to rename the file to

ps i have changed it to 'newname'; my fault :s but it still dont work
Copy linkTweet thisAlerts:
@k0r54authorAug 27.2004 — hi, i have changed bits but its still the same says its done but there nothing ?

any idea's what im doin wrong
Copy linkTweet thisAlerts:
@k0r54authorAug 28.2004 — Hi, been up all night messing about with it, i hate it when ya cant sleep coz ya cant figure it out lol.

Well any idea's im back to this code above again now.

thanks adam
Copy linkTweet thisAlerts:
@Kr_ZAug 28.2004 — [code=php]
<?php

$new_name = "example/test"; // The script adds the right extension

foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";

$tmp_name = $file_array['tmp_name'];
$filename = $file_array['name'];

if(is_uploaded_file($tmp_name)) {

$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];

move_uploaded_file($tmp_name, $new_name.".".$ext) or die("Couldn't copy");
echo "file was moved!<br><br>";

}
}

?>
[/code]
Copy linkTweet thisAlerts:
@k0r54authorAug 28.2004 — Hi,

its still not uploading the file, or atleast it says it is and there is no error at all and the echo's show but the is no file in the dir :s

Sorry :s

any more idea's
Copy linkTweet thisAlerts:
@k0r54authorAug 28.2004 — ok, i have actually got somewhere

using this code

[code=php]
<?php
$new_name = "examples/"; // The script adds the right extension

//just to display the outcome
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";

$tmp_name = $file_array['tmp_name'];
$filename = $file_array['name'];

if(is_uploaded_file($tmp_name)) {

$ext = explode("me", $filename);

//this should break down the extenions
$ext = $ext[count($ext)-1];

move_uploaded_file($tmp_name, $new_name."me".$ext) or die("Couldn't copy");
echo "file was moved!<br><br>";

}
}
?>
[/code]


ok, now if i upload test.txt it will upload the file to metest.txt now if some1 could just perhaps break this down that would be great
Copy linkTweet thisAlerts:
@Kr_ZAug 28.2004 — I don't see exactly what you want.

Do you want to add "me" to the beginning of the filename? Or replace everything before the extension?
Copy linkTweet thisAlerts:
@k0r54authorAug 28.2004 — Hey i have finally managed to do it ?

AT LOOOONNNGGGGG LAST!

ok here it the final code, any mods you may be able to do please let me know as it all helps ?

im qute glad i done this one.. Thanks all

[code=php]
<?php

//this is the directory it will go into
$dir = "examples/";

//this is the name of the file
$new_name = "$_POST[newname]";

//this is what to display if it is all correct
foreach($_FILES as $file_name => $file_array) {
echo "Path: ".$file_array['tmp_name']."<br>n";
echo "Name: ".$file_array['name']."<br>n";
echo "Type: ".$file_array['type']."<br>n";
echo "Size: ".$file_array['size']."<br>n";

//this sets the temp name and the name as a variable
$tmp_name = $file_array['tmp_name'];
$filename = $file_array['name'];

//this uploads the file
if(is_uploaded_file($tmp_name)) {

//this code seperates the uploaded file so i can retreive the extension
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];

//moves from the temp directory to the loacation i want and changes the name
move_uploaded_file($tmp_name, $dir.$new_name.".".$ext) or die("Couldn't copy");
echo "file was moved!<br><br>";

}
}

?>
[/code]
Copy linkTweet thisAlerts:
@k0r54authorAug 28.2004 — Hi, sorry to perhaps kick all this off again, but is there a way with the code i am using to display the full URL location hence http://www.domainname.com/blah/blah/blah.txt

like that

Many thanks

Adam
×

Success!

Help @k0r54 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...