adamlacombe Posted July 10, 2009 Share Posted July 10, 2009 I have this script: <?php if($_SESSION['id']){ ?> <form action="user.php?action=avatar" method="post" enctype="multipart/form-data"> <div class="header">Upload Avatar</div><br /> <div class="content"> Avatar:<br /> <input name='tuploaded' type='file' /><br /> <input type="submit" name="update" value="Upload"> </form> </div> <? if($_POST[update]){ $ttarget1 = "avatars/"; $ttarget2 = $ttarget1 . basename( $_FILES['tuploaded']['name']); $tfile=($_FILES['tuploaded']['name']); $sql3 ="UPDATE `users` SET `avatar`='$tfile' WHERE `id`='".$_SESSION['id']."'"; $res3 = mysql_query($sql3) or die(mysql_error()); if(move_uploaded_file($_FILES['tuploaded']['tmp_name'], $ttarget2)){ echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Your Avatar has been successfully uploaded!\"); window.location=\"index.php\"; </script>"; } } }else{ echo "<div class='error'>Please log in first</div><br />"; } ?> but I want to make it so when a user uploads an avatar it is renamed and uploaded as their username and I also want to make sure the file is under 1mb. How would i go about doing this? Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/ Share on other sites More sharing options...
ignace Posted July 10, 2009 Share Posted July 10, 2009 How would i go about doing this? 1) read the manual 2) come back if you have a php problem Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-872981 Share on other sites More sharing options...
adamlacombe Posted July 10, 2009 Author Share Posted July 10, 2009 ummm what manual? ??? Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-872983 Share on other sites More sharing options...
ignace Posted July 10, 2009 Share Posted July 10, 2009 http://www.php.net/manual/en/features.file-upload.php explains you how to upload files and restrict them. use copy() to rename the uploaded file to the username's name: http://be.php.net/manual/en/function.copy.php Here you go Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-872988 Share on other sites More sharing options...
adamlacombe Posted July 10, 2009 Author Share Posted July 10, 2009 ooh ok. I looked at it and im still very confused about how to do it, this is what I came up with after reading it: <?php if($_SESSION['id']){ ?> <form action="user.php?action=avatar" method="post" enctype="multipart/form-data"> <div class="header">Upload Avatar</div><br /> <div class="content"> Avatar:<br /> <input name='tuploaded' type='file' /><br /> <input type="submit" name="update" value="Upload"> </form> </div> <? if($_POST[update]){ $ttarget1 = "avatars/"; $ttarget2 = $ttarget1 . basename( $_SESSION['id'].jpg); $tfile=($_SESSION['id'].jpg); $sql3 ="UPDATE `users` SET `avatar`='$tfile' WHERE `id`='".$_SESSION['id']."'"; $res3 = mysql_query($sql3) or die(mysql_error()); if(move_uploaded_file($_FILES['tuploaded']['tmp_name'], $ttarget2)){ echo "<script language=\"Javascript\" type=\"text/javascript\"> alert(\"Your Avatar has been successfully updated!\"); window.location=\"index.php\"; </script>"; } } }else{ echo "<div class='error'>Please log in first</div><br />"; } ?> it renames it to the users id but when it uploads it does not say for example 1.jpg it leaves out the period. Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-873053 Share on other sites More sharing options...
J.Daniels Posted July 10, 2009 Share Posted July 10, 2009 Try this: $ttarget2 = $ttarget1 . $_SESSION['id'] . 'jpg'; Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-873058 Share on other sites More sharing options...
ignace Posted July 10, 2009 Share Posted July 10, 2009 Try this: $ttarget2 = $ttarget1 . $_SESSION['id'] . 'jpg'; $ttarget2 = $ttarget1 . $_SESSION['id'] . '.jpg'; Note the . before jpg Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-873069 Share on other sites More sharing options...
adamlacombe Posted July 10, 2009 Author Share Posted July 10, 2009 i did that and the same thing is happening.. Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-873073 Share on other sites More sharing options...
adamlacombe Posted July 10, 2009 Author Share Posted July 10, 2009 nvm.. fixed! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/165516-solved-upload-file-rename-and-restrict-size/#findComment-873080 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.