image-upload-script

Gesponsorde koppelingen

PHP script bestanden

  1. image-upload-script

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
// Author: Ruben Vandenbussche
// Website: http://www.RVandenbussche.nl
// Contact: info (at) RVandenbussche (dot) nl
// Date: 11 Nov 2008
//*******************************************
// Script: Image Upload v1.0
//It does:
//  1.  Upload the image.
//  2.  Rename the image.
//  3.  Move the image.
//  4.  Rename if already exists.
//  5.  Check if image is valid if not it gets deleted
//*
//It doesn't:
//  1.  Upload other than .JPG, .JPEG, .GIF and .PNG.
//  2.  Do anything to the image itself.
//  3.  Create directories
//*******************************************

// Summary:
// Function to upload an image from the formfield ($img_ff) to a specified path ($dst_path), check the image and give it an
// other name ($dst_img).

//Script start here.


function uploadImage($img_ff, $dst_path, $dst_img){

    //Get variables for the function.
            //complete path of the destination image.

    $dst_cpl = $dst_path . basename($dst_img);
            //name without extension of the destination image.
    $dst_name = preg_replace('/\.[^.]*$/', '', $dst_img);
            //extension of the destination image without a "." (dot).
    $dst_ext = strtolower(end(explode(".", $dst_img)));

//Check if destination image already exists, if so, the image will get an extra number added.
    while(file_exists($dst_cpl) == true){
        $i = $i+1;
        $dst_img = $dst_name . $i . '.' . $dst_ext;
        $dst_cpl = $dst_path . basename($dst_img);
    }


        //upload the file and move it to the specified folder.
    move_uploaded_file($_FILES[$img_ff]['tmp_name'], $dst_cpl);

        //get type of image.
    $dst_type = exif_imagetype($dst_cpl);

        //Checking extension and imagetype of the destination image and delete if it is wrong.
    if(( (($dst_ext =="jpg") && ($dst_type =="2")) || (($dst_ext =="jpeg") && ($dst_type =="2")) || (($dst_ext =="gif") && ($dst_type =="1")) || (($dst_ext =="png") && ($dst_type =="3") )) == false){
        unlink($dst_cpl);
        die('<p>The file "'. $dst_img . '" with the extension "' . $dst_ext . '" and the imagetype "' . $dst_type . '" is not a valid image. Please upload an image with the extension JPG, JPEG, PNG or GIF and has a valid image filetype.</p>');
    }
}

    //Script ends here.


// Needed for the function:

        // If the form is posted do this:

    if($_SERVER['REQUEST_METHOD'] == 'POST'){

            //Variables needed for the function.
        $img_ff = 'image'; // Form name of the image
        $dst_img = strtolower($_FILES[$img_ff]['name']); // This name will be given to the image. (in this case: lowercased original image name uploaded by user).
        $dst_path = '../directory/'; // The path where the image will be moved to.

        uploadImage($img_ff, $dst_path, $dst_img);
    }




 //HTML Starts Here.

<!-- // Form needed to upload the image.
     // You can change the name of the form, dont forget to change that in the variable $img_ff.
     // You can change the action file. (In this case i use the same name as this file.)-->

<form enctype="multipart/form-data" name="image" method="post" action="EmptyPHP.php">
    <
label for="image">Image:</label>
    <
input type="file" id="image" name="image" />
    <
br />
    <
input type="submit" value="Upload" />&nbsp;<input type="reset" value="Reset" />
<
/form>

//HTML Ends Here .
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.