Simple Upload Form + Change filename

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

Post Reply
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Simple Upload Form + Change filename

Post by thiscatis »

I's experimenting with form uploads,
I'm using this code:

Code: Select all

if($_POST['Submit']) { 

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
} }
I'd like to change filename when the file is uploaded to a predefined variable.
How do I do this? I think the tmp_name needs to change somehow but I have no clue how...
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Simple Upload Form + Change filename

Post by tecktalkcm0391 »

Not tested but you can try this:

Code: Select all

if($_POST['Submit']) { 

$target_path = "uploads/";
$newname = time();

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path) && rename($target_path.$_FILES['uploadedfile']['tmp_name'], $target_path.$newname)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
} }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

tmp_name doesn't need to change, the $target_path needs to change.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

hey feyd,
i did it like this now:

Code: Select all

if($_FILES[picture_1][name] !="") {

	$image_name = $_FILES[picture_1][name] ;
	$extension = ".jpg";
	
	$newimg_Name =  "$m_id" . $extension; // m_id is defined before this snippet
	$newimg_Path = "modules/uploads/" . $newimg_Name;
	

	move_uploaded_file($_FILES[picture_1][tmp_name], $newimg_Path);
	chmod($newimg_Path, 0777);
}
And it works!

But an additional question,

is it possible to upload if to 2 different locations at once?
adding a move_uploaded_file doesn't work.. also adding the entire code again with a new $newimg_Path
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Quote your named array indices. ;)

Once you perform move_uploaded_file() you will need to copy() to create another instance of the file.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

can it be copied to a directory lower than the one the php script is in?
If so, do I need to use /../dir/ or ../dir/ ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Try it.
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post by thiscatis »

Code: Select all

if($_FILES[picture_1][name] !="") {

	$image_name = $_FILES['picture_1']['name'] ;
	$extension = ".jpg";
	$newimg_Name =  "$m_id" . $extension;
	$newimg_Path = "modules/models/" . $newimg_Name;
	move_uploaded_file($_FILES['picture_1']['tmp_name'], $newimg_Path);
	chmod($newimg_Path, 0777);
	if (!copy($newimg_Name, "../modules/models/$newimg_Name")) {
   echo "Error. Couldn't copy that file.\n";}

}
Keeps giving me "Error. Couldn't copy that file".
Is the way I set the new path correctly?
I had a look at rename() but that removes the original picture from the directory too.
Maybe first a

Code: Select all

if (!copy($newimg_Name, "$newimg_Name.bak")) {
   echo "Error. Couldn't copy that file.\n";}
and then a

Code: Select all

rename()
or is there a better way to do this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're missing quotes for a new more named indices.

The copy call needs $newimg_Path, not $newimg_Name, for the first parameter.
Post Reply