1<?php
2 if(isset($_FILES['image'])){
3 $errors= array();
4 $file_name = $_FILES['image']['name'];
5 $file_size =$_FILES['image']['size'];
6 $file_tmp =$_FILES['image']['tmp_name'];
7 $file_type=$_FILES['image']['type'];
8 $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
9
10 $extensions= array("jpeg","jpg","png");
11
12 if(in_array($file_ext,$extensions)=== false){
13 $errors[]="extension not allowed, please choose a JPEG or PNG file.";
14 }
15
16 if($file_size > 2097152){
17 $errors[]='File size must be excately 2 MB';
18 }
19
20 if(empty($errors)==true){
21 move_uploaded_file($file_tmp,"images/".$file_name);
22 echo "Success";
23 }else{
24 print_r($errors);
25 }
26 }
27?>
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<?php
2 if(isset($_FILES['image'])){
3 $errors= array();
4 $file_name = $_FILES['image']['name'];
5 $file_size =$_FILES['image']['size'];
6 $file_tmp =$_FILES['image']['tmp_name'];
7 $file_type=$_FILES['image']['type'];
8 $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
9
10 $extensions= array("jpeg","jpg","png");
11
12 if(in_array($file_ext,$extensions)=== false){
13 $errors[]="extension not allowed, please choose a JPEG or PNG file.";
14 }
15
16 if($file_size > 2097152){
17 $errors[]='File size must be excately 2 MB';
18 }
19
20 if(empty($errors)==true){
21 move_uploaded_file($file_tmp,"images/".$file_name);
22 echo "Success";
23 }else{
24 print_r($errors);
25 }
26 }
27?>
28<html>
29 <body>
30
31 <form action="" method="POST" enctype="multipart/form-data">
32 <input type="file" name="image" />
33 <input type="submit"/>
34 </form>
35
36 </body>
37</html>