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

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

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

Post by hward »

i gained an extension but no jpg
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

i mean a period but no jpg behind it
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Can you show the full code again?
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

<?

if ($img1_name != "") {

$parts = pathinfo($img1);
@copy("$img1", "pictures/$new_name.$parts[extension]")
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>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Try this

Code: Select all

<? 

if ($img1_name != "") { 

$parts = pathinfo($img1); 
@copy("$img1", "pictures/'.$new_name.$parts[extension].'") 
or die("Couldn't copy the file."); 

} else { 

die("No input file specified"); 

} 

?>
I've just added the '. bits to cover the variables
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

got the file name as '.12..'
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

should have came out 12.jpg
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just do this instead. Don't bother with pathinfo()

Code: Select all

<?php

if ($img1_name != "") { 

$parts = explode('.', $Img1);

array_reverse($parts);

@copy("$img1", "pictures/'.$new_name.$parts[0].'") 
or die("Couldn't copy the file."); 

} else { 

die("No input file specified"); 

} 

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

Post by hward »

i got it to work

<?

if ($img1_name != "") {

$parts = pathinfo($img1_name);
@copy("$img1", "pictures/$new_name.$parts[extension]")
or die("Couldn't copy the file.");

} else {

die("No input file specified");

}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

he'll still get .12.(maybe jpg).

he's using a double quoted string, not single quotes.

hward, change those single quotes to double quotes, and it should come out okay..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

:oops: Oops. We've been working with $Img all along when we should have been using $Img_name lol
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

it seems to be working fine now on all extensions with that last change i just posted
hward
Forum Contributor
Posts: 107
Joined: Mon Apr 19, 2004 6:19 pm

Post by hward »

i took out the periods that I was getting too is that going to cause me any problems
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No, they shouldn't have been there anyway. That was my mistake. You only need them when you're working inside single quotes.
Post Reply