Uploading Files In MySql Using PHP

Discussion in 'PHP' started by minhazikram, Dec 29, 2011.

  1. #1
    This is the form through which i will upload my file with a picture. But i have no idea of how to process this in php

    <html>
    <head></head>
    <body>

    <form enctype="multipart/form-data" action="process.php" method="post">
    Name:<input type="text" name="name">
    Photo :<input type="file" name="photo">
    <input type="submit" name="submit" value="submit">
    <form>
     
    minhazikram, Dec 29, 2011 IP
  2. Kenneth_Da

    Kenneth_Da Active Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #2
    Do you want upload file to host and save file location to mysql ?
     
    Kenneth_Da, Dec 29, 2011 IP
  3. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #3
    as per my experience , nothing gets memorized more than something done by yourself

    so .. here is a tutorial for you http://www.w3schools.com/php/php_file_upload.asp

    read it, follow the steps, make your mistakes, correct them, and learn


    P.S. If you are about to save the file content into your mysql database instead of a file on the server, here is what you can do

    instead of this
    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
    PHP:
    you can do ....
    1. create a blob 64kb (or better longblob 50mb) column in your mysql table (let's name it file_content)
    2. $content = file_get_contents($_FILES["file"]["tmp_name"]);
    3. mysql_query("INSERT INTO `mytable` (`filename`, `file_content`) VALUES ('".mysql_real_escape_string($_FILES["file"]["tmp_name"])."', '".mysql_real_escape_string($content)."')");

    pps.
    print_r the $_FILES array so you can see if you need some more data out of there
     
    stats, Dec 29, 2011 IP
  4. GregH

    GregH Greenhorn

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    I wouldnt suggest uploading the file directly to mysql database. Will make the db very large!

    But yes. The tutorial above is quite a good one to start with. You can build on that one easily enough if you wish too.
    Save the actual file on the server and maybe use the mysql database to have the key information such as file location, file name, file size and so on?
     
    GregH, Dec 29, 2011 IP
  5. webzexpert

    webzexpert Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I always prefer to store file name in the mysql table and upload file to the server in the folder, I wont prefer to store file in binary format in mysql table
     
    webzexpert, Dec 30, 2011 IP
  6. sadhan.digital

    sadhan.digital Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hello, I am telling you step by step:

    Create a folder called My_files into the same directory where the script will be uploaded and provide chmode 0777 to the directory.

    write this code into your top of the page

    <?php
    if(isset($_POST["submit"]))
    {
    if(move_uploaded_file($_FILES["file"]["tmp_name"], "my_files/" . $_FILES["file"]["name"]))
    {
    echo "The file has been successfully uploaded.";
    }
    }
    ?>
     
    sadhan.digital, Dec 30, 2011 IP
  7. shahinfosoft

    shahinfosoft Greenhorn

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    yes it is very easy

    using php function move_upload_file($_FILES['photo']['tmpname'], "upload_img/" . $_FILES["photo"]["name"]);

    //heare first argument is your temp. path of your file and another is destination path which u want to move a file.
     
    shahinfosoft, Dec 30, 2011 IP
  8. WPC

    WPC Peon

    Messages:
    116
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    This is extemely easy you can find many php tutorials on the internet by using google and youtube to learn how to write your own, this way its better to write your own than use others because you can make modifications and know where and what to fix, and where to add modifications etc.
     
    WPC, Dec 31, 2011 IP