PHP - Upload Formular: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 1: | Zeile 1: | ||
| − | == Beispiel 1 - ohne AJAX == | + | == Beispiel 1 - Upload Formular ohne AJAX == |
http://php.net/manual/de/features.file-upload.post-method.php | http://php.net/manual/de/features.file-upload.post-method.php | ||
=== HTML === | === HTML === | ||
| Zeile 110: | Zeile 110: | ||
echo("</pre>"); | echo("</pre>"); | ||
?> | ?> | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | == Beispiel 2 - Upload Formular mit Mailversandt == | ||
| + | PHP Ergänzung: | ||
| + | <syntaxhighlight lang="php"> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Version vom 28. Januar 2016, 17:52 Uhr
Beispiel 1 - Upload Formular ohne AJAX
http://php.net/manual/de/features.file-upload.post-method.php
HTML
Formular mit
enctype="multipart/form-data"
und
<input type="file" name="uploaded_file">
<!DOCTYPE html>
<html>
<body>
<form method="POST" name="email_form_with_php"
action="upload.php" enctype="multipart/form-data">
<label for='name'>Name: </label>
<input type="text" name="name" >
<label for='email'>Email: </label>
<input type="text" name="email" >
<label for='message'>Message:</label>
<textarea name="message"></textarea>
<label for='uploaded_file'>Select A File To Upload:</label>
<input type="file" name="uploaded_file">
<input type="submit" value="Submit" name='submit'>
</form>
</body>
</html>
Das maxfilesize feld ist hier nicht drin. Es ersetzt keine servervalidierung hilft soll aber dem user schon vor dem upload helfen zu erkennen, wenn er ein zu großes File nimmt - theoretisch denn in der Praxis unterstützt es kein Browser.
PHP
Auf die frisch hochgeladenen Dateien kann über die Servervariable $_FILES zugegriffen werden
<?php
echo("<pre>");
$upload_ok = 1;
$errors = '';
$strDate=date("Y-m-d_H:i:s_");
/***** CONFIGURATION PART *****/
$actual_path = dirname($_SERVER["SCRIPT_FILENAME"]);
$upload_folder = $actual_path.'/uploads/';
$max_allowed_file_size = 10000; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "png");
//FILE INFOS
/*
echo('$_FILES Variable');
var_dump($_FILES);
echo('$_SERVER Variable');
var_dump($_SERVER);
*/
$name_of_uploaded_file = $strDate.basename($_FILES['uploaded_file']['name']);
//get the file extension of the file
$type_of_uploaded_file =
substr($name_of_uploaded_file,
strrpos($name_of_uploaded_file, '.') + 1);
$size_of_uploaded_file =
$_FILES["uploaded_file"]["size"]/1024;//size in KBs
/***** VALIDATE UPLOADED FILE *****/
//Size validation
if($size_of_uploaded_file > $max_allowed_file_size )
{
$errors .= "<br> Size of file should be less than $max_allowed_file_size";
$upload_ok = 0;
}
//File extension validation
$allowed_ext = false;
for($i=0; $i < sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
$errors .= "<br> The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
$upload_ok = 0;
}
/***** COPY TEMPORARY UPLOADED FILE *****/
$target_path_and_name = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if($upload_ok && is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$target_path_and_name))
{
$errors .= '<br>error while copying the uploaded file';
$upload_ok = 0;
}
}
if (!$upload_ok) echo $errors;
else echo("upload ok");
echo("</pre>");
?>
Beispiel 2 - Upload Formular mit Mailversandt
PHP Ergänzung: