Foto toevoeg script

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Gertjan Gielen

Gertjan Gielen

30/12/2012 14:05:20
Quote Anchor link
Hallo allemaal,
Ik wil mensen images laten uploaden op mijn website. Nu heb ik daar een script voor gevonden op phphulp. Maar nu wil ik daar graag een functie voor toevoegen. Dat mensen bijvoorbeeld een eigen naam kunnen bedenken met daarbij hun ingame naam (het is een website voor minecraftserver). Kan iemand mij hier bij helpen?
Ik heb al het form voor aangepast.

Groetjes
Gertjan

Quote:
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
<?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 = '../screenshots/'; // 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" />
<label for="image">Minecraftnaam:</label>
<input type="file" id="ign" name="ign" />
<br />
<input type="submit" value="Upload" />&nbsp;<input type="reset" value="Reset" />
</form>
 
PHP hulp

PHP hulp

15/05/2024 09:32:52
 
Bo Ter Ham

Bo Ter Ham

30/12/2012 14:21:34
Quote Anchor link
'Dat mensen bijvoorbeeld een eigen naam kunnen bedenken met daarbij hun ingame naam (het is een website voor minecraftserver). Kan iemand mij hier bij helpen?'

en wat wil je dan met die naam gaan doen?
 
Joop Slabbekoorn

Joop Slabbekoorn

30/12/2012 14:27:40
Quote Anchor link
Plaats in je form een invoerveld voor naam
In je script staat ie er toch al bij?

<input type='text' id="name" name="name" />

Toevoeging op 30/12/2012 14:29:11:

Joop Slabbekoorn op 30/12/2012 14:27:40:
Plaats in je form een invoerveld voor naam
In je script staat ie er toch al bij?

<input type='text' id="name" name="name" />


Simpeler kan niet toch?
 
Gertjan Gielen

Gertjan Gielen

30/12/2012 16:16:05
Quote Anchor link
Excuses, misschien had ik niet duidelijk gemaakt wat ik er mee wilde:P
Ik wil dat de ingame naam wordt toegevoegd aan de file.

Bijvoorbeeld:
Ik wil een plaatje uploaden. Deze heeft nu de naam screenshot1.png

Bij het uploaden vul ik de naam in het form. Nu moet de file gerenamed worden naar screenshot1_gebruikersnaam.png
Hierdoor kan ik later deze informatie weer gebruiken in de slideshow en kan ik zien wie bepaalde bestanden heeft geupload.

Mocht het nog niet duidelijk zijn, hoor ik dat graag.
 
Joop Slabbekoorn

Joop Slabbekoorn

30/12/2012 16:58:07
Quote Anchor link
Oké zo sorry iets in deze koers dan

$filenaamoud = $img_ff
$gebruiker = $_SESSION['gebruikersnaam'];
$filenaamnieuw = '$filenaamoud'_'$gebruiker';

$dst_img = strtolower($_FILES[$img_ff][$filenaamnieuw]);

Zal niet helemaal goed zijn maar ik denk dat je zoiets bedoelt
 
Wouter J

Wouter J

30/12/2012 17:50:05
Quote Anchor link
Joop, inderdaad je moet variabele altijd uit quotes halen en wat je met '$filenaamoud'_'$gebruiker' bedoelt?

Tevens hebben we hier code blocks + highlighting, zou je die willen gebruiken?
 
Gertjan Gielen

Gertjan Gielen

30/12/2012 18:34:56
Quote Anchor link
Misschien had ik het script uberhaupt moeten testen of het werkte voordat ik dingen ging aanpassen. Het werkte niet:P

In ieder geval bedankt voor de hulp!
Gewijzigd op 30/12/2012 18:36:22 door Gertjan Gielen
 
Joop Slabbekoorn

Joop Slabbekoorn

30/12/2012 18:43:12
Quote Anchor link
Wouter J op 30/12/2012 17:50:05:
Joop, inderdaad je moet variabele altijd uit quotes halen en wat je met '$filenaamoud'_'$gebruiker' bedoelt?

Tevens hebben we hier code blocks + highlighting, zou je die willen gebruiken?


Hij wou dus dat het bestand zo ging heten zegmaar $img_ff en dan _ $gebruiker en $ img_ff hebben we $filenaamoud genoemd dus moet de nieuwe naam $filenaamoud_$gebruiker worden zegmaar

Daarom zei ik al zal niet helemaal goed wezen
 



Overzicht Reageren

 
 

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.