22-03-2014, 20:52
Kod PHP:
<?php
session_start();
require("config.php");
/*
*
* @ Multiple File upload script.
*
* @ Can do any number of file uploads
* @ Just set the variables below and away you go
*
* @ Author: Kevin Waterson
*
* @copywrite 2008 PHPRO.ORG
*
*/
error_reporting(E_ALL);
/*** the upload directory ***/
$upload_dir= '/uploads/';
/*** numver of files to upload ***/
$num_uploads = 2;
/*** maximum filesize allowed in bytes ***/
$max_file_size = 1048576;
/*** a message for users ***/
$msg = 'Please select files for uploading';
/*** an array to hold messages ***/
$messages = array();
/*** check if a file has been submitted ***/
if(isset($_FILES['userfile']['tmp_name']))
{
/** loop through the array of files ***/
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
{
// check if there is a file in the array
if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i]))
{
$messages[] = 'No file uploaded @';
}
// check the file is less than the maximum file size
elseif($_FILES['userfile']['size'][$i] > $max_file_size)
{
$messages[] = "File size exceeds $max_file_size limit";
}
else
{
// copy the file to the specified dir
if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$_SERVER['DOCUMENT_ROOT'].$upload_dir.$_FILES['userfile']['name'][$i]))
{
/*** give praise and thanks to the php gods ***/
$messages[] = $_FILES['userfile']['name'][$i].' uploaded';
}
else
{
/*** an error message ***/
$messages[] = 'Uploading '.$_FILES['userfile']['name'][$i].' Failed';
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>
<div class="message">
<h3><?php echo $msg; ?></h3>
<p>
<?php
if(sizeof($messages) != 0)
{
foreach($messages as $err)
{
echo $err.'<br />';
}
}
?>
</p>
</div>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
<p>News Title:</p>
<input type="text" name="article_title"/><br />
<p>Wybierz zdjęcia:</p>
<?php
$num = 0;
while($num < $num_uploads)
{
echo '<input type="file" name="userfile[]"><br />';
$num++;
}
?>
<br />
<input type="submit" name="submit">
<input type="reset" value="Reset">
</form>
</body>
</html>