1//This is the minimal code for an image upload for first time learners
2//html portion
3<!DOCTYPE html>
4<html>
5<head>
6 <title>ImageUpload</title>
7</head>
8<body>
9 <form action="upload.php" method="post" enctype="multipart/form-data">
10 <label>Username</label>
11 <input type="text" name="username">
12 <br>
13 <label>UploadImage</label>
14 <input type="file" name='myfile'>
15 <br/>
16 <input type="submit" value="upload">
17 </form>
18</body>
19</html>
20
21 //php portion
22 <?php
23 $user=$_POST['username'];
24 $image=$_FILES['myfile'];
25 echo "Hello $user <br/>";
26 echo "File Name<b>::</b> ".$image['name'];
27
28 move_uploaded_file($image['tmp_name'],"photos/".$image['name']);
29 //here the "photos" folder is in same folder as the upload.php,
30 //otherwise complete url has to be mentioned
31 ?>
1
2 if(isset($_FILES['image']))
3 {
4 $img_name = $_FILES['image']['name']; //getting user uploaded name
5 $img_type = $_FILES['image']['type']; //getting user uploaded img type
6 $tmp_name = $_FILES['image']['tmp_name']; //this temporary name is used to save/move file in our folder.
7
8 // let's explode image and get the last name(extension) like jpg, png
9 $img_explode = explode(".",$img_name);
10 $img_ext = end($img_explode); //here we get the extension of an user uploaded img file
11
12 $extension= ['png','jpeg','jpg','gif']; //these are some valid img extension and we are store them in array.
13
1
2<?php
3if(isset($_POST['btn-upload']))
4{
5
6 $file = rand(1000,100000)."-".$_FILES['file']['name'];
7 $file_loc = $_FILES['file']['tmp_name'];
8 $file_size = $_FILES['file']['size'];
9 $file_type = $_FILES['file']['type'];
10 $folder="uploads/";
11
12 move_uploaded_file($file_loc,$folder.$file);
13 $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$file','$file_type','$file_size')";
14 mysql_query($sql);
15}
16?>
1<?php
2/*
3echo "<pre>";
4echo "FILES:<br>";
5print_r ($_FILES );
6echo "</pre>";
7*/
8if ( $_FILES['uploaddatei']['name'] <> "" )
9{
10 // Datei wurde durch HTML-Formular hochgeladen
11 // und kann nun weiterverarbeitet werden
12
13 // Kontrolle, ob Dateityp zulässig ist
14 $zugelassenedateitypen = array("image/png", "image/jpeg", "image/gif");
15
16 if ( ! in_array( $_FILES['uploaddatei']['type'] , $zugelassenedateitypen ))
17 {
18 echo "<p>Dateitype ist NICHT zugelassen</p>";
19 }
20 else
21 {
22 // Test ob Dateiname in Ordnung
23 $_FILES['uploaddatei']['name']
24 = dateiname_bereinigen($_FILES['uploaddatei']['name']);
25
26 if ( $_FILES['uploaddatei']['name'] <> '' )
27 {
28 move_uploaded_file (
29 $_FILES['uploaddatei']['tmp_name'] ,
30 'hochgeladenes/'. $_FILES['uploaddatei']['name'] );
31
32 echo "<p>Hochladen war erfolgreich: ";
33 echo '<a href="hochgeladenes/'. $_FILES['uploaddatei']['name'] .'">';
34 echo 'hochgeladenes/'. $_FILES['uploaddatei']['name'];
35 echo '</a>';
36 }
37 else
38 {
39 echo "<p>Dateiname ist nicht zulässig</p>";
40 }
41 }
42}
43
44function dateiname_bereinigen($dateiname)
45{
46 // erwünschte Zeichen erhalten bzw. umschreiben
47 // aus allen ä wird ae, ü -> ue, ß -> ss (je nach Sprache mehr Aufwand)
48 // und sonst noch ein paar Dinge (ist schätzungsweise mein persönlicher Geschmach ;)
49 $dateiname = strtolower ( $dateiname );
50 $dateiname = str_replace ('"', "-", $dateiname );
51 $dateiname = str_replace ("'", "-", $dateiname );
52 $dateiname = str_replace ("*", "-", $dateiname );
53 $dateiname = str_replace ("ß", "ss", $dateiname );
54 $dateiname = str_replace ("ß", "ss", $dateiname );
55 $dateiname = str_replace ("ä", "ae", $dateiname );
56 $dateiname = str_replace ("ä", "ae", $dateiname );
57 $dateiname = str_replace ("ö", "oe", $dateiname );
58 $dateiname = str_replace ("ö", "oe", $dateiname );
59 $dateiname = str_replace ("ü", "ue", $dateiname );
60 $dateiname = str_replace ("ü", "ue", $dateiname );
61 $dateiname = str_replace ("Ä", "ae", $dateiname );
62 $dateiname = str_replace ("Ö", "oe", $dateiname );
63 $dateiname = str_replace ("Ü", "ue", $dateiname );
64 $dateiname = htmlentities ( $dateiname );
65 $dateiname = str_replace ("&", "und", $dateiname );
66 $dateiname = str_replace (" ", "und", $dateiname );
67 $dateiname = str_replace ("(", "-", $dateiname );
68 $dateiname = str_replace (")", "-", $dateiname );
69 $dateiname = str_replace (" ", "-", $dateiname );
70 $dateiname = str_replace ("'", "-", $dateiname );
71 $dateiname = str_replace ("/", "-", $dateiname );
72 $dateiname = str_replace ("?", "-", $dateiname );
73 $dateiname = str_replace ("!", "-", $dateiname );
74 $dateiname = str_replace (":", "-", $dateiname );
75 $dateiname = str_replace (";", "-", $dateiname );
76 $dateiname = str_replace (",", "-", $dateiname );
77 $dateiname = str_replace ("--", "-", $dateiname );
78
79 // und nun jagen wir noch die Heilfunktion darüber
80 $dateiname = filter_var($dateiname, FILTER_SANITIZE_URL);
81 return ($dateiname);
82}
83?>
84
85<form name="uploadformular"
86 enctype="multipart/form-data" action="dateiupload.php" method="post">
87Datei: <input type="file" name="uploaddatei" size="60" maxlength="255">
88<input type="Submit" name="submit" value="Datei hochladen">
89</form>
1// To change: FILENAME, array with allowed extensions, Max Filesite, Filepath
2if(upload("FILENAME", array("jpeg","jpg","png"), 209715, "C:/xampp/htdocs/")){
3 echo "Success";
4 }
5
6
7function upload($f_name, $f_ext_allowed, $f_maxsize, $f_path){
8
9 $f_name_2 = $_FILES[$f_name]['name'];
10 $f_size = $_FILES[$f_name]['size'];
11 $f_tmp = $_FILES[$f_name]['tmp_name'];
12 $f_error = $_FILES[$f_name]['error'];
13 $f_ext = strtolower(end(explode('.',$f_name_2)));
14 $f_rename = $_SESSION['uid'] . "." . $f_ext;
15
16 if($f_error == 0 && in_array($f_ext, $f_ext_allowed)
17 && $f_size < $f_maxsize && mb_strlen($f_name_2, "UTF-8") < 225
18 && preg_match("`^[-0-9A-Z_\.]+$`i", $f_name_2)){
19 if(move_uploaded_file($f_tmp, $f_path . $f_name_2){
20 return true;
21 }else{
22 return false;
23 }
24 }else{
25 return false;
26 }
27}