Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: File Upload

  1. #1
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    File Upload

    Hi

    upload.php
    PHP Code:
    <?
     
    // 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
    Code:
    <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
    -------------------------
    Shailesh Patil
    -------------------------

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    upload.php
    PHP Code:
    <?
     
    // 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!";
    }
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •