uploading a file and changing its name
Moderator: General Moderators
uploading a file and changing its name
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> </H1>
<FORM METHOD="post" ACTION="do_upload.php" ENCTYPE="multipart/form-data">
<p> </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>
This is the code I am working with.
***********************************************************
<HTML>
<HEAD>
<TITLE>Upload a File</TITLE>
</HEAD>
<BODY>
<H1> </H1>
<FORM METHOD="post" ACTION="do_upload.php" ENCTYPE="multipart/form-data">
<p> </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
change
to
where $newName is teh new name for the file
Code: Select all
@copy("$img1", "pictures/$img1_name")Code: Select all
@copy("$img1", "pictures/$newName")-
Illusionist
- Forum Regular
- Posts: 903
- Joined: Mon Jan 12, 2004 9:32 pm
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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.
This should work
Code: Select all
@copy("$img1", "pictures/$newName")
change it to
$extension = substr($orignalfilename, 0, -4);
@copy("$img1", "pictures/'.$newName.$extension.'");- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Whoaaa man.... got that back to front its just
sorry
Code: Select all
$extension = substr($originalfilename, -3);- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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...
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.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
You just need to define a variable as the pathinfo() of your file and then echo the extension bit onto the new filename.
I've never used that function but that's how it makes it look in the manual
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'].'") ;
Last edited by Chris Corbyn on Mon Apr 19, 2004 7:45 pm, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$parts = pathinfo($oldname);
@copy("$img1", "pictures/$newName.$parts[extension]")