Jump to content

Renaming


Solar

Recommended Posts

How can I make it so when someone uploads an image, that it will rename the file to their name. Eg. URL/photo/Solar.gif

 

I can't figure it out :(

 

<?php 
$username = $_SESSION['username'];
$target = "./photo/";
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/140680-renaming/
Share on other sites

change $target

$target = $target . basename( $_FILES['uploaded']['name']) ;

 

to

 


$target = "./photo/";
$path = pathinfo($_FILES['uploaded']['name']); //gather file data
$targetnew = $target.$_SESSION['username'].$path['extension'];
while(file_exists($targetnew))
{ //make sure the file doesnt already exist
  $targetnew = $target.rand(0,1000).$_SESSION['username'].$path['extension'];
}
$target = $targetnew;unset($targetnew);

 

I havnt tested but looks ok to me.

 

Link to comment
https://forums.phpfreaks.com/topic/140680-renaming/#findComment-736245
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.