Friday, July 20, 2012

Rename file php


Rename file php
When you upload a file into a folder having a file name same with uploading file renaming of file is essential.
create folder name as upload.
The file php-big.jpg has been uploaded
File Name :4438
  • 1006read2.php
  • 1153xa3.png
  • 1654php-big.jpg
  • 3854write.php
  • 3880read2.php
  • 4438php-big.jpg
  • 4660php-big.jpg
  • 6499php-big.jpg
  • 660php-big.jpg
  • 7956xa3.png
  • read.php
  • write.php

  • <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form action="uploader.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td><strong>Single File Upload </strong></td>
    </tr>
    <tr>
    <td>Select file
    <input name="ufile" type="file" id="ufile" size="50" /></td>
    </tr>
    <tr>
    <td align="center"><input type="submit" name="Submit" value="Upload" /></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>

    uploader.php

    <html>
    <body>
    <?php
    $file_name = $_POST_FILES['ufile']['name'];
    $random_digit=rand(0000,9999);
    $new_file_name=$random_digit.$file_name;
    $target_path = "upload/".$new_file_name;

    $target_path = $target_path . basename( $_FILES['ufile']['name']);

    if(move_uploaded_file($_FILES['ufile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['ufile']['name']).
    " has been uploaded</br>";
    echo "File Name :".$new_file_name."<BR/>";

    } else{
    echo "There was an error uploading the file, please try again!";
    }

    ?>

    <?php

     $folder = "upload/";
    $handle = opendir($folder);
    while ($file = readdir($handle))
    {
     $files[ ] = $file;
    }
    closedir($handle);
    foreach ($files as $file)
     {
      echo "<tr bgcolor=#ffffff>
        <td align='center'></td>";
    echo '<li><a href="upload/'.$file.'">'.$file.'</a></li>';
    }
    ?>

    </body>
    </html>

    No comments:

    Post a Comment