...

View Full Version : File Upload



shaileshpatil
11-18-2006, 01:21 PM
Hi

upload.php


<?

// Where the file is going to be placed
$target_path = "../files/";
/* 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'];
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!";
}
?>

upload.html


<form enctype="multipart/form-data" name="form1" action="upload.php" method="POST">
<table cellpadding=1 cellspacing=3 class="table" width="60%" border=0>
<tr>
<td class="r" width="30%">Choose a file to upload: </td>
<td ><input name="uploadedfile" type="file"></td>
</tr>
<tr>
<td class="r" colspan="2" align="center"><input type="submit" value="Upload File"></td>
</tr>
<input type="hidden" name="MAX_FILE_SIZE" value="100000">

</table>
</form>


This scripts works absolutely fine. It uploads the file to specific directories.
While uploading i want to rename a file with same extension it has. The purpose of doing so, is to avoid files being overwritten.
Any suggestions

boeing747fp
11-18-2006, 11:20 PM
upload.php


<?

// Where the file is going to be placed
$target_path = "../files/";
/* 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'];
if(file_exists($target_path)){
//already a file with this name... do something!!!
//Add a UNIX TIMESTAMP to the filename!
$target_path = "../files/".time()."_".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!";
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum