Page 1 of 3

uploading a file and changing its name

Posted: Mon Apr 19, 2004 6:19 pm
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>

Posted: Mon Apr 19, 2004 6:38 pm
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

Posted: Mon Apr 19, 2004 6:50 pm
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

Posted: Mon Apr 19, 2004 6:56 pm
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

Posted: Mon Apr 19, 2004 7:11 pm
by Illusionist
you can get teh original extension, using [php_man]pathinfo()[/php_man], then add it on to the new name.

Posted: Mon Apr 19, 2004 7:17 pm
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

Posted: Mon Apr 19, 2004 7:18 pm
by Chris Corbyn
Whoaaa man.... got that back to front its just

Code: Select all

$extension = substr($originalfilename, -3);
sorry

Posted: Mon Apr 19, 2004 7:20 pm
by feyd
be careful with that, not all files have a 3 character extension.

Posted: Mon Apr 19, 2004 7:26 pm
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()

Posted: Mon Apr 19, 2004 7:27 pm
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...

Posted: Mon Apr 19, 2004 7:30 pm
by Chris Corbyn
Yeah or array_reverse() and take first element

Posted: Mon Apr 19, 2004 7:35 pm
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

Posted: Mon Apr 19, 2004 7:42 pm
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

Posted: Mon Apr 19, 2004 7:42 pm
by feyd

Code: Select all

$parts = pathinfo($oldname);
@copy("$img1", "pictures/$newName.$parts[extension]")

Posted: Mon Apr 19, 2004 7:43 pm
by feyd
heh, beat me to it! :P