file upload & file renaming

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
ggrrr
Forum Newbie
Posts: 3
Joined: Sun May 28, 2006 12:43 pm
Location: England

file upload & file renaming

Post by ggrrr »

I need help! Basically I’ve got the upload form mastered and working because it is mega simple. But its php where I get stuck. The upload works but can when the files are saved onto the server in the uploads/ folder is there a php script to change the name of any file going in to one name. :?: :?:

I made the upload thingy so that it only accepts .jpg but cant change the name


plz help me sum1 8O

Lots of love ggrrr
:) :) :) :) :)
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Post by someberry »

What is your code at the moment, it's kinda hard to just guess the problem, especially from your description ;)
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

rename it in the copy or move_uploaded_file

i.e.

Code: Select all

$tempFile = $_FILES['name of file object']['tmp_name'];
$uploadDir = "./uploads/";
$fileExtension = pathinfo($_FILES['name of file object']['tmp_name'], PATHINFO_EXTENSION)

$newFilename = date("d-m-Y_H:i") . $fileExtension;

copy ($tempFile, $uploadDir . $newFilename);

// or

move_uploaded_file ($tempFile, $uploadDir . $newFilename);
in theory, should work, I haven't tested it though. I use that sort of setup and works for me.
ggrrr
Forum Newbie
Posts: 3
Joined: Sun May 28, 2006 12:43 pm
Location: England

ggrrr

Post by ggrrr »

twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


wow 2 replies people acctuall care awww isnt that nice 

thankz if you want to see my code so far its pretty simple but it works

Code: Select all

<?php
$target_path = "uploads/";

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

$_FILES['uploadedfile']['tmp_name']; 

$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!";
} 
?>
just to explain bit more i wanted something to add in so the files that are uploaded into the folder are renamed when they are put on the server.

thankz for the script ill see if it werks or if i fail (i will most likely fail) but that wont stop me :)

merry christmas from ggrrr


twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

just change the target path, it'll move it to a new filename.

Code: Select all

// Get file extension so there shouldn't be any unexpected errors
$fileExtension = pathinfo($_FILES['name of file object']['tmp_name'], PATHINFO_EXTENSION);

// Include the new name in the target_path variable
$target_path = $target_path . date("d-m-Y_H:i") . "." . $fileExtension;

     // Example $target_path "uploads/28-05-2006_11:15.png"
*$target_path edited to include a '.' between the name and extension (forgot to in the last example).
ggrrr
Forum Newbie
Posts: 3
Joined: Sun May 28, 2006 12:43 pm
Location: England

YEY

Post by ggrrr »

thankz its working most exelently now.

randomly goes in the wrong place but thats my fault i think i took & deleted some of the script but ill soon fix that :)

anyway im done now but ill probbably be back begging for help again

:P :P :P :P :P
Post Reply