Rename To Upload File in PHP

when a upload files to automatically  rename files using time and file name  to encrypt the file and store to database


PHP code for Rename to file name


$filename=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$pos = strrpos($filename, '.');
if($pos === false)
$ext = "";
$ext = substr($filename, $pos);
$time=time();
$newFilename=md5($filename.$time).$ext; // Rename file name
move_uploaded_file($temp,'files/' . $newFilename) ; //Rename file name with upload

FULL CODING



$insert=mysql_query("insert into upload(image_name)values('$newFilename')")or die(mysql_error());

<?php
$conn=mysql_connect("localhost","root","") or die(mysql_error());
$sdb=mysql_select_db("demo",$conn) or die(mysql_error());

if(isset($_POST['submit'])!="")
{
$filename=$_FILES['photo']['name'];
$size=$_FILES['photo']['size'];
$type=$_FILES['photo']['type'];
$temp=$_FILES['photo']['tmp_name'];
$pos = strrpos($filename, '.');
if($pos === false)
$ext = "";
$ext = substr($filename, $pos);
$time=time();
$newFilename=md5($filename.$time).$ext;
move_uploaded_file($temp,'files/' . $newFilename) ;

$insert=mysql_query("insert into upload(image_name)values('$newFilename')")or die(mysql_error());
if($insert){
}
else{
die(mysql_error());
}
}
?>
<html>
<head>
<title>Rename To Upload File in PHP</title>
</head>
<style>
body{ font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;}
a{color:#666;}
#table{margin:0 auto;background:#333;box-shadow: 5px 5px 5px #888888;border-radius:10px;color:#CCC;padding:10px;}
#table1{margin:0 auto;}
</style>
<body>
<h2><a href="http://crackerworld.blogspot.in/">Cracker World</a></h2>
<div style="border:1px solid #CCC;"></div>

<h3><p align="center">Rename To Upload File</p></h3>
<form enctype="multipart/form-data" action="" name="form" method="post">
<table border="0" cellspacing="0" cellpadding="5" id="table">
<tr>
<th >Chosse Files</th>
<td ><label for="photo"></label><input type="file" name="photo" id="photo" required/></td>
</tr>
<tr>
<th colspan="2" scope="row"><input type="submit" name="submit" id="submit" value="Submit" /></th>
</tr>
</table>
</form>
<br />
<br />
<table border="1" align="center" id="table1" cellpadding="0" cellspacing="0" width="400px;">
<tr><td align="center"><strong>Files</strong></td></tr>
<?php
$select=mysql_query("select * from upload order by id desc");
while($row1=mysql_fetch_array($select)){
$image_name=$row1['image_name'];
?>
<tr>
<td>
&nbsp;&nbsp;&nbsp;<a href="files/<?php echo $image_name ;?>"><img src="files/arrow1.png" width="14" height="14"><?php echo $image_name ;?></a>
</td>
<?php }?>
</table>

</body>
</html>

No comments:

Post a Comment