Friday, September 26, 2014

Photo Upload / Upload Foto

Kali ini saya akan menjelaskan cara untuk membuat halaman upload foto. Untuk membuatnya kita membutuhkan 2 file. Buatlah file dengan nama upload.php dan upload_file.php, kemudian buatlah 1 folder bernama "upload" tempat menyimpan foto yang telah di-upload tadi.

File upload.php nantinya akan memanggil fungsi upload_file.php untuk mengambil gambar dan menyimpannya dalam folder yang telah kita tentukan.

Berikut source code upload.php:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<html>
    <body>
    <center>
        <form action="upload_file.php" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td><label for="file">Filename:</label></td>
                    <td><input type="file" name="file" id="file"><br></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value="Submit"/><br/></td>
                </tr>
            </table>
        </form>
    </center>
    </body>
</html> 


source code upload_file.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
<!DOCTYPE html>
<html>
    <head>
        <title>Upload</title>
    </head>
    <body>
    <center>
        <?php
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["file"]["name"]);
        $extension = end($temp);

        echo $_FILES["file"]["type"];
        echo $_FILES["file"]["size"];
        ;

        if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/jpg")
                || ($_FILES["file"]["type"] == "image/pjpeg")
                || ($_FILES["file"]["type"] == "image/x-png")
                || ($_FILES["file"]["type"] == "image/png"))
                && ($_FILES["file"]["size"] < 2000000)
                && in_array($extension, $allowedExts)) {

            if ($_FILES["file"]["error"] > 0) {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            } else {
                echo "Upload: " . $_FILES["file"]["name"] . "<br>";
                echo "Type: " . $_FILES["file"]["type"] . "<br>";
                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
                if (file_exists("upload/" . $_FILES["file"]["name"])) {
                    echo $_FILES["file"]["name"] . " <br/><br/>already exists. ";
                } else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                    echo "<br/><br/>Stored in: " . "upload/" . $_FILES["file"]["name"] . "<br/><br/>";
                }
            }
        } else {
            echo "Invalid file";
        }
        ?> 
        <img width="200px" src="upload/<?php echo $_FILES["file"]["name"]; ?>"></img>
    </center>
</body>
</html>


Tampilan hasilnya adalah sebagai berikut:





No comments:

Post a Comment