Jump to content

[SOLVED] upload file, rename, and restrict size.


adamlacombe

Recommended Posts

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?

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 ;)

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.

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.