Kadin
August 31, 2010, 7:06pm
1
I had this thing working for a single image. I then added:
$counter = 3;
for ($i = 0; $i < $counter; $i++) { to surround the code that already works. }
I then changed the form at the bottom which might be wrong.
Can someone spot the obvious that I can’t see. Thanks.
<?php
define ('MAX_FILE_SIZE', 3000000);
define('UPLOAD_DIR', 'upload_test/');
$counter = 3;
if( isset($_POST['upload']) ){
for ($i = 0; $i < $counter; $i++) {
if (!empty($_FILES['img']['name'])){
$imgnm = $_FILES['img']['name'];
move_uploaded_file($_FILES['img']['tmp_name'], UPLOAD_DIR.$imgnm);
$udimgnm = UPLOAD_DIR.$imgnm;
$typeok = TRUE;
switch($_FILES['img']['type']){
case "image/gif": $src = imagecreatefromgif($udimgnm); break;
case "image/jpeg": // Both regular and progressive jpegs
case "image/pjpeg": $src = imagecreatefromjpeg($udimgnm); break;
case "image/png": $src = imagecreatefrompng($udimgnm); break;
default: $typeok = FALSE; break;
}
if($typeok){
list($w, $h) = getimagesize($udimgnm);
$max = 100;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w)
{
$th = $max / $w * $h;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array( // Sharpen image
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1)
), 8, 0);
imagejpeg($tmp, $udimgnm);
imagedestroy($tmp);
imagedestroy($src);
}else
echo "<p>error with image upload.</p>";
}
}
}
?>
<form action="post_s3.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
<?php // Create the inputs.
for ($i = 0; $i < $counter; $i++) {
echo '<p><input type="file" name="img" /></p>';
}
?>
<!--<input type="hidden" name="submitted" value="TRUE" />-->
<input type="submit" value="upload image" name="upload"/>
</form>
Kadin
September 2, 2010, 6:40pm
2
Why must there be brackets “ ”? I’ve never seen this before.
name=“file ”
Ernie1
September 1, 2010, 6:09pm
3
Kadin:
I notice there is no submit button.
<form name=“form” enctype=“multipart/form-data” method=“post” action=“test.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%;”>
<?php for ($i = 0; $i < $counter; $i++){ ?>
<input type=“file” name=“file ” /><br />
<?php } ?>
<span id=“file_1”></span><br />
</td>
</tr>
</table>
So I added this.
<input type=“submit” value=“upload image” name=“upload”/>
</form>
I can’t seem to make this work. Does name=“file ” need to be placed where ‘img’ is above?
if (!empty($_FILES[‘img’][‘name’])){
So it looks like this for all the ‘img’ s.
if (!empty($_FILES[‘file ’][‘name’])){
Thanks.
The new form should be used with the modified version of the script that I gave you in post #2
quick easy way to upload multiple items… and you can add your own php script in to process the files how ever you want.
Kadin
August 31, 2010, 8:45pm
5
I notice there is no submit button.
<form name=“form” enctype=“multipart/form-data” method=“post” action=“test.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%;”>
<?php for ($i = 0; $i < $counter; $i++){ ?>
<input type=“file” name=“file ” /><br />
<?php } ?>
<span id=“file_1”></span><br />
</td>
</tr>
</table>
So I added this.
<input type=“submit” value=“upload image” name=“upload”/>
</form>
I can’t seem to make this work. Does name=“file ” need to be placed where ‘img’ is above?
if (!empty($_FILES[‘img’][‘name’])){
So it looks like this for all the ‘img’ s.
if (!empty($_FILES[‘file ’][‘name’])){
Thanks.
Ernie1
August 31, 2010, 7:44pm
6
Try this:
upload.js
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>\
';
}
}
<?php
define('MAX_FILE_SIZE', 3000000);
define('UPLOAD_DIR', 'upload_test/');
$counter = 3;
if (isset($_POST['upload']))
{
for ($i = 0; $i < $counter; $i++)
{
if (!empty($_FILES['file']['name'][$i]))
{
$filenm = $_FILES['file']['name'][$i];
move_uploaded_file($_FILES['file']['tmp_name'][$i], UPLOAD_DIR . $filenm);
$udfilenm = UPLOAD_DIR . $filenm;
$typeok = true;
switch ($_FILES['file']['type'][$i])
{
case "image/gif":
$src = imagecreatefromgif($udfilenm);
break;
case "image/jpeg":
// Both regular and progressive jpegs
case "image/pjpeg":
$src = imagecreatefromjpeg($udfilenm);
break;
case "image/png":
$src = imagecreatefrompng($udfilenm);
break;
default:
$typeok = false;
break;
}
if ($typeok)
{
list($w, $h) = getimagesize($udfilenm);
$max = 100;
$tw = $w;
$th = $h;
if ($w > $h && $max < $w)
{
$th = $max / $w * $h;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$tmp = imagecreatetruecolor($tw, $th);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imageconvolution($tmp, array(// Sharpen image
array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
imagejpeg($tmp, $udfilenm);
imagedestroy($tmp);
imagedestroy($src);
}
else
echo "<p>error with image upload.</p>";
}
}
}
?>
<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="test.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" 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="upload" value="submit" />
</form>
</body>
Ernie1
September 2, 2010, 12:29pm
7
Even better:
<form name="form" enctype="multipart/form-data" method="post" action="test.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%;">
<?php for ($i = 0; $i < 3; $i++){
echo "File " . $i ?> <input type="file" name="file[]" /><br />
<?php } ?>
<span id="file_1"></span><br />
</td>
</tr>
</table>
<input type="submit" name="upload" value="submit" />
</form>
Kadin
September 2, 2010, 8:45pm
8
Adding the brackets automatically creates an array server-side containing the entries.
I see.
Kadin
September 2, 2010, 8:43pm
9
I guess name=“file ” holds $i in all the ($_FILES[‘file’][‘name’][$i]), but the brackets are empty, “file ”.
Adding the brackets automatically creates an array server-side containing the entries.
Kadin
August 31, 2010, 8:24pm
11
I don’t know what that is.
Kadin
August 31, 2010, 8:15pm
12
Thanks.
That looks a lot simpler. I will need some time to study and test it.
Ernie1
August 31, 2010, 8:10pm
14
Maybe this:
<form name="form" enctype="multipart/form-data" method="post" action="test.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%;">
<?php for ($i = 0; $i < $counter; $i++){ ?>
<input type="file" name="file[]" /><br />
<?php } ?>
<span id="file_1"></span><br />
</td>
</tr>
</table>
Kadin
August 31, 2010, 7:53pm
16
Thanks for your response.
Is JavaScript required to make this work? I don’t like using JavaScript because I am concerned it may be disabled in the clients browser plus I don’t understand JavaScript enough yet to deal with it. Thanks for your suggestion and I will study it.
Can anyone else see how this might work?