Senin, 19 November 2012

  • Scrip PHP UPload file

    PHP Script to eat:

    This script tells you that with PHP, it is possible to upload files to the server. In this script(PHP File Upload Script), two files are developed. Files are named as:
    1. upload_file_form.php
    2. upload_file_script.php
    This tutorial follows a diagram, If one understand this diagram, its easy to develop a script. Because you are clear what you are going to do and in how many steps. So let’s move to the diagram:
    Following the diagram, First step is to create a table on your database. Creating a table is very easy in MySQL UI(You can visit my tutorials about MySQL to know how to create table). Here, simply copy this query in SQL box.
    To allow user to upload a file from form may prove very useful. Look at the following form to upload a file from the user’s local machine.

    Code: upload_file_form.php

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Upload File Form</title>
    </head>
     
    <body>
     
    <form id="form1" name="form1" enctype="multipart/form-data" method="post" action="file_upload_script.php">
    <h2>PHP Script to Upload File</h2>
    Upload File:
    <input type="file" name="fileField" id="fileField" />
     
    <input type="submit" name="btnAdd" id="btnAdd" value="Upload" />
    </form>
    </body>
    </html>
    Once it is ready, you have to develop PHP script to complete this. Before start developing PHP script, i would like to shed some light on  the procedure that i am going to follow. Here in this tutorial, i have changed the name of the original file, before uploading it to the server, reason is that there may be chance of existance of a file with same name on the server already. The best solution is to change the name of every file, one is going to upload.

    Code: upload_file_script.php

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    <?php
    //=============Configuring Server and Database=======
    $host = 'localhost';
    $user = 'root';
    $password = 'vertrigo';
    //=============Data Base Information=================
    $database = 'dbsneaker';
     
    $conn = mysql_connect($host,$user,$password) or die('Server Information is not Correct'); //Establish Connection with Server
    mysql_select_db($database,$conn) or die('Database Information is not correct');
     
    //===============End Server Configuration============
     
    if(isset($_POST['btnAdd']))
    {
    $myFile = $_FILES['fileField']['name']; // Storing name into variable
     
    //====If you want to change the name of the File====
    $anyNum = rand(20,500789000); //Will generate a random number between 20and 500789000
     
    $newFileName = $anyNum.$myFile;//===New string is concatenated====
    //===Following Function will check if the File already exists========
     
    if (file_exists("upload/".$newFileName))
    {
    echo $newFileName." already exists. ";
    }
    //******If file already exists in your Folder, It will return zero and Will not take any action===
    //======Otherwise File will be stored in your given directory and Will store its name in Database===
    else
    {
     
    $query = "insert into tblfileupload(file_name) values ('$newFileName')";
     
    $res = mysql_query($query);
     
    copy($_FILES['fileField']['tmp_name'],'upload/'.$newFileName);
    //===Copy File Into your given Directory,copy(Source,Destination)
     
    if($res>0)
    {
    //====$res will be greater than 0 only when File is uploaded Successfully====:)
    echo 'You have Successfulluy Uploaded File';
    }
    }
    }
    ?>
    When the file is uploaded to your server, let us have look at the table on database, as an experience i have uploaded three files with same name and each time i have got different results.
  • 0 komentar:

    Posting Komentar

    Copyright @ 2014 Diary Studio | The Best Solution | Jasa Pembuatan Website.

    Designed by Diary | TechTabloids