Renaming Picture, or adding to the 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

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Renaming Picture, or adding to the name.

Post by nickman013 »

I have a picture upload script.

The form:

Code: Select all

<form action=picture.php method=post>
<input type="file" name=uploadedfile>
<inpu type=submit>
</form>

The processor:

Code: Select all

$linktopic = $_FILES['uploadedfile']['name']; 
// Where the file is going to be placed 
$target_path = "/home/muot/public_html/pages/muotreport/submitted/";

/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

// This is how we will get the temporary file...
$_FILES['uploadedfile']['tmp_name'];
$target_path = "/home/muot/public_html/pages/muotreport/submitted/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "S";
} else{
echo "E";
}
I need to be able to add to the name or change the name of the picture. I know there is a way to change the name in that script, i just dont want to mess anything up.

Thanks!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I've added what you requested, cleaned it up and fixed some stuff.

The form:

Code: Select all

<form action="picture.php" method="post" enctype="multipart/form-data">
	<input type="file" name="uploadedfile" /><br />
	File name to use (w/ extension): <input type="text" name="newfilename" /><br />
	<input type="submit" name="submit" value="Submit" />
</form>
The processor:

Code: Select all

<?php

//Where the file is going to be placed
$uploaddir = "uploads/";

//Set the target for the file to be uploaded to
$target = $uploaddir . $_POST['newfilename'];

//Upload the file
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) {
	echo 'File uploaded.';
} else {
	echo 'File upload error.';
}

?>
Obviously some sort of validation should be added to the processing script but I've just given you the basics.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thanks alot, it is perfect!

I just got a Parse error, but i fixed it!

Thanks Again!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

No probs, but can I just ask which line the parse error was on? I can't see it in the code I gave you :?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

yes, it was this line

Code: Select all

} else {
I think you had one to many brackets, but I dont know lol.


Thanks Alot :D
Post Reply