Rename image name to username

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
edgars
Forum Newbie
Posts: 1
Joined: Sun Feb 24, 2008 3:48 am

Rename image name to username

Post by edgars »

Hi! How I can in this script rename image neme to username? Plzz help, I'm new in php!

Code: Select all

<?
//print_r($_POST);
 
if($_POST["action"] == "Upload Image")
{
unset($data['user_name']);
 
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
 
if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";
 
 
$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;
 
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
 
if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
 
}
 
?>
 
 
<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<p><input type="file" name="image_file" size="20"></p>
<p><input type="submit" value="Upload Image" name="action"></p>
</form>
 
<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
 
$orig["dir"] = "images/";
$orig["dirimage"] = $orig["dir"] . $imagename;
$orig["res"] = @imagecreatefromjpeg($orig["dirimage"]);
 
$orig["x"] = imagesx($orig["res"]);
$orig["y"] = imagesy($orig["res"]);
 
if($orig["x"] > $orig["y"])
{
$new["x"] = 165;
$new["y"] = (165 / $orig["x"]) * $orig["y"];
}
else
{
$new["y"] = 165;
$new["x"] = (165 / $orig["y"] ) * $orig["x"];
}
 
//switch out imagecreatetruecolor with imagecreate if using gd version 1
//$new["res"] = imagecreate($new["x"],$new["y"]);
$new["res"] = imagecreatetruecolor($new["x"],$new["y"]);
 
//set background to white
$fill = imagecolorallocate($new["res"], 255, 255, 255);
imagefill($new["res"], 0, 0, $fill);
imagecopyresized($new["res"], $orig["res"], 0, 0, 0, 0, $new["x"], $new["y"], $orig["x"], $orig["y"]);
 
$new["dir"] = "images/";
$new["dirimage"] = $new["dir"] . $imagename . ".thumb.jpg";
 
imagejpeg($new["res"], $new["dirimage"]);
 
imagedestroy($orig["res"]);
imagedestroy($new["res"]);
?>
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Rename image name to username

Post by markusn00b »

When you do the move_uploaded_file, the second paramater is the new name of the file.
So therefore, if you have the username (sessions?) you can just plop this in - prefixed with the relevant directory.

Code: Select all

 
// We need to find out what the relevant file extension is:
switch ($_FILES['uploaded_file']['type'])
{
    case "image/jpg":
        $_ext = ".jpg";
        break;
    case "image/gif";
        $_ext = ".gif";
        break;
    // you can continue this...
}
$_username = $_SESSION['user_name']; // username from session
$_dir = "images/";
    /** Move uploaded image TMP_NAME -> "images/whatever_username_is **/
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $_dir . $_username . $_ext) or die("Error moving file..");
 
 
Post Reply