Wer eine schlanke PHP-Klasse zum Hochladen seiner Dateien sucht, der kann hier eventuell fündig werden. Für den produktiven Einsatz müsste dieser Schnipsel aber sicherlich noch erweitert werden. Ich empfehle, mindestens bezüglich den Endbenutzer-Fehlermeldungen noch Anpassungen vorzunehmen. Wer Interesse hat, kann den nachfolgenden Quelltext gerne unter Berücksichtigung der GNU AGPL weiterverwenden und/oder erweitern.
<?php
/**
* Another lightweight upload class, written in PHP.
*
* @author Pascal Hollenstein <webmaster@zockerade.com>
* @version 1.1
* @license GNU AGPL
*/
class Upload {
/**
* This variable contains the upload destination directory.
*
* @access private
* @var string
*/
private $destination = "";
/**
* This variable contains the file name after the successful upload.
*
* @access private
* @var string
*/
private $name = "";
/**
* This variable contains the MIME type of the upload file.
*
* @access private
* @var string
*/
private $type = "";
/**
* This variable contains the file size of the upload file.
*
* @access private
* @var integer
*/
private $size = 0;
/**
* This variable contains the valid MIME types for the upload process.
*
* @access private
* @var array
*/
private $mimeTypeList = array();
/**
* This method checks if the current file is valid or not.
*
* @access private
* @param void
* @return boolean
*/
private function isValid() {
if ($this->getMaxSize() > $this->size) {
if (in_array($this->type, $this->mimeTypeList)) {
return true;
}
}
return false;
}
/**
* This method tries to return the maximum file size in bytes.
*
* @access private
* @param void
* @return mixed
*/
private function getMaxSize() {
if (function_exists("ini_get")) {
$option = ini_get("upload_max_filesize");
if (!empty($option)) {
switch (substr($option, -1)) {
case "G": $option *= 1024;
case "M": $option *= 1024;
case "K": $option *= 1024;
}
return (int) $option;
}
}
return false;
}
/**
* This method returns the file name after the successful upload.
*
* @access public
* @param void
* @return mixed
*/
public function getFileName() {
if (file_exists($this->destination.$this->name)) {
return $this->name;
}
return false;
}
/**
* This procedure tries to set the valid MIME types.
*
* @access public
* @param array $mimeTypes
* @return void
*/
public function setMimeTypes($mimeTypes) {
if (is_array($mimeTypes)) {
$this->mimeTypeList = $mimeTypes;
}
}
/**
* This procedure tries to set the upload destination directory.
*
* @access public
* @param string $destination
* @return void
*/
public function setFileDestination($destination) {
if (is_string($destination)) {
$this->destination = $_SERVER["DOCUMENT_ROOT"].$destination;
}
}
/**
* This method tries to upload a file.
*
* @access public
* @param array $fileToUpload
* @return boolean
*/
public function performUpload($toUpload) {
$arrayKeys = array("name", "tmp_name", "size", "type");
if (is_array($toUpload)) {
$arrayDiff = array_diff_key(array_flip($arrayKeys), $toUpload);
if (count($arrayDiff) === 0) {
$this->name = time()."-".urlencode($toUpload["name"]);
$this->size = $toUpload["size"];
$this->type = $toUpload["type"];
if ($this->isValid() && is_writable($this->destination)) {
return move_uploaded_file(
$toUpload["tmp_name"],
$this->destination.$this->name
);
}
}
}
return false;
}
}
?>
<?php
include "/path/to/the/upload/class/file.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Upload</title>
</head>
<body>
<h1>Upload</h1>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Please choose your file to upload!</legend>
<p>
<input type="file" name="file">
<input type="submit" value="Submit">
</p>
<?php
if (isset($_FILES["file"])) {
$upload = new Upload;
$upload->setMimeTypes(array(
"image/png",
"image/jpeg",
"image/gif"
));
$upload->setFileDestination("/absolute/path/to/the/upload/directory/");
if ($upload->performUpload($_FILES["file"])) {
echo 'The file "'.$upload->getFileName().'" was successfully uploaded!';
} else {
echo "The upload failed!";
}
}
?>
</fieldset>
</form>
</body>
</html>