Maybe this will help you:
upload.js
Code JavaScript:
function add_file(id, i)
{
if (document.getElementById(id + '_' + i).innerHTML.search('uploadinputbutton') == -1)
{
document.getElementById(id + '_' + i).innerHTML = '<input type="file" class="uploadinputbutton" maxsize="" name="' + id + '[]" onchange="return add_file(\'' + id + '\', ' + (i+1) + ');" /><br /><span id="' + id + '_' + (i+1) + '"><input type="button" value="Add other" onclick="add_file(\'' + id + '\', ' + (i+1) + ');" /><\/span>\n';
}
}
upload.html
Code HTML4Strict:
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="upload.js"></script>
</head>
<body>
<form name="form" enctype="multipart/form-data" method="post" action="upload.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="vertical-align: top; width: 50%;">
<div class="header">Files</div><div style="font-size: 90%;">Files entered here will be transferred to the server.</div><br />
</td>
</tr>
<tr>
<td style="vertical-align: top; width: 50%;">
<input type="file" class="uploadinputbutton" name="file[]" onchange="add_file('file', 1);" /><br />
<span id="file_1"><input type="button" value="Add another" onclick="add_file('file', 1);" /></span><br />
</td>
</tr>
</table>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
upload.php
PHP Code:
<?php
include ( "im.class.php" );
header( "Content-type: text/html" );
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );
echo "<?xml version=\"1.0\"?>\n";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
?>
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="upload.js"></script>
</head>
<body>
<?php
if ( isset($_POST['submit']) )
{
$obj = new processImage( "file", "user_album", "thumbs", 120, 75 );
}
?>
<a title="album" href="album.php">+ album</a>
</body>
</html>
img.class.php
PHP Code:
<?php
class processImage
{
public function createThumbnail( $srcFile, $destFile, $width, $quality = 75 )
{
$thumbnail = '';
if ( file_exists($srcFile) && isset($destFile) )
{
$size = getimagesize( $srcFile );
$w = number_format( $width, 0, ',', '' );
$h = number_format( ($size[1] / $size[0]) * $width, 0, ',', '' );
$thumbnail = self::copyImage( $srcFile, $destFile, $w, $h, $quality );
}
// return the thumbnail file name on sucess or blank on fail
return basename( $thumbnail );
}
public function copyImage( $srcFile, $destFile, $w, $h, $quality = 75 )
{
$tmpSrc = pathinfo( strtolower($srcFile) );
$tmpDest = pathinfo( strtolower($destFile) );
$size = getimagesize( $srcFile );
if ( $tmpDest['extension'] == "gif" )
{
$destFile = substr_replace( $destFile, 'gif', -3 );
$dest = imagecreatetruecolor( $w, $h );
}
elseif ( $tmpDest['extension'] == "jpg" )
{
$destFile = substr_replace( $destFile, 'jpg', -3 );
$dest = imagecreatetruecolor( $w, $h );
}
elseif ( $tmpDest['extension'] == "jpeg" )
{
$destFile = substr_replace( $destFile, 'jpeg', -4 );
$dest = imagecreatetruecolor( $w, $h );
}
elseif ( $tmpDest['extension'] == "png" )
{
$dest = imagecreatetruecolor( $w, $h );
}
else
{
return false;
}
switch ( $size[2] )
{
case 1: // GIF
$src = imagecreatefromgif( $srcFile );
break;
case 2: // JPG
$src = imagecreatefromjpeg( $srcFile );
break;
case 3: // PNG
$src = imagecreatefrompng( $srcFile );
break;
default:
return false;
break;
}
imagecopyresampled( $dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1] );
switch ( $size[2] )
{
case 1:
imagegif( $dest, $destFile );
break;
case 2:
imagejpeg( $dest, $destFile, $quality );
break;
case 3:
imagepng( $dest, $destFile );
}
return $destFile;
}
public function execute( $k, $path, $thumbs, $size )
{
foreach ( $_FILES[$k]['error'] as $key => $error )
{
if ( $error == UPLOAD_ERR_OK )
{
$img = "$path/" . $_FILES[$k]['name'][$key];
$t = 1;
while ( file_exists($img) )
{
$img = $path . "/" . $_FILES[$k]['name'][$key];
$img = substr( $img, 0, strpos($img, ".") ) . "-$t" . strstr( $img, "." );
$t++;
}
$tmp_name = $_FILES[$k]['tmp_name'][$key];
$mf = move_uploaded_file( $tmp_name, $img );
$x = explode( "/", $img );
$y = $path . "/" . $thumbs . "/" . end( $x );
self::createThumbnail( $img, $y, $size );
if ( $mf )
{
echo $_FILES[$k]['name'][$key] . " uploaded successfully<br />\n";
}
else
{
echo $_FILES[$k]['name'][$key] . " upload failed<br />\n";
}
}
}
}
}
?>
Bookmarks