uploading a file and changing its name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

uploading a file and changing its name

Post by hward »

I was wondering if someone could help me, I am trying to upload a files but change the name of the file to what is typed into a textarea from another part of the page. I am new to php and just starting out. I have search the new for code like I am wanting but can't find just what I need.

This is the code I am working with.

***********************************************************
<HTML>
<HEAD>
<TITLE>Upload a File</TITLE>
</HEAD>
<BODY>

<H1>&nbsp;</H1>

<FORM METHOD="post" ACTION="do_upload.php" ENCTYPE="multipart/form-data">

<p>&nbsp;</p>

<p><b>New Name</b>:<input type="text" name="new_name" size="20"></p>

<p><strong>File to Upload:</strong><br>
<INPUT TYPE="file" NAME="img1" SIZE="30"></p>

<P><INPUT TYPE="submit" NAME="submit" VALUE="Upload File"></p>

</FORM>

</BODY>
</HTML>


**********************************************************

<?

if ($img1_name != "") {

@copy("$img1", "pictures/$img1_name")
or die("Couldn't copy the file.");

} else {

die("No input file specified");

}

?>

<HTML>
<HEAD>
<TITLE>Successful File Upload</TITLE>
</HEAD>
<BODY>

<H1>Success!</H1>

<P>You sent: <? echo "$img1_name"; ?>, a <? echo "$img1_size"; ?>
byte file with a mime type of <? echo "$img1_type"; ?>.</P>

</BODY>
</HTML>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

change

Code: Select all

@copy("$img1", "pictures/$img1_name")
to

Code: Select all

@copy("$img1", "pictures/$newName")
where $newName is teh new name for the file
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

tried that before putting the

@copy("$img1", "pictures/$newName")

$new_name to the same as the textarea i want for the name but it didn't seem to do anything different. uploads the file fine but still has orginal name
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

i take that back i just checked in the folder and it is doing it only problem is that it is removing the file extension so when uploading a jpg it uploads it without the extension thats while I wasn't able to pull it up and see it

how do i tell it to keep the original extension
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

you can get teh original extension, using [php_man]pathinfo()[/php_man], then add it on to the new name.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You can take the extension from the original name using substr($originalfilename, 0, -4) and then stick this onto the end of the new name.

Code: Select all

@copy("$img1", "pictures/$newName") 

change it to

$extension = substr($orignalfilename, 0, -4);
@copy("$img1", "pictures/'.$newName.$extension.'");
This should work
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Whoaaa man.... got that back to front its just

Code: Select all

$extension = substr($originalfilename, -3);
sorry
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

be careful with that, not all files have a 3 character extension.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd, that's true. pathinfo() is probably best then. I was thinking of explode('.', $Img1) too but then you need to make sure the user does not add '.' anywhere to the filename. Just go with pathinfo()
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

could explode on period using the last nonempty element as the extension..

edit: safer still would be to also exclude non alphanumeric and most non-keyboard symbols. Although you'd want to keep accented characters too..depending on the end goal...
Last edited by feyd on Mon Apr 19, 2004 7:33 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah or array_reverse() and take first element
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

could someone show me how you would use the pathinfo(), in there I am reading in the manual but not quite getting it
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You just need to define a variable as the pathinfo() of your file and then echo the extension bit onto the new filename.

Code: Select all

$parts = pathinfo($img1);  /*You may need to write the path to the file too like pathinfo('pictures/'.Img1.''); */

@copy("$img1", "pictures/'.$newName.$parts['extension'].'") ;
I've never used that function but that's how it makes it look in the manual
Last edited by Chris Corbyn on Mon Apr 19, 2004 7:45 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$parts = pathinfo($oldname);
@copy("$img1", "pictures/$newName.$parts[extension]")
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

heh, beat me to it! :P
Post Reply