这是上传包含说明的文件的代码。文件传输没有问题。我不知道如何使用它来上传多个文件。我想发送2个文件。我尝试了很多方法来解决自己的方式,但我全力以赴:)提前感谢您的帮助。
<form enctype="multipart/form-data"
action="upload_image.php" method="post">
<input type="hidden"
name="MAX_FILE_SIZE" value="524288">
<fieldset><legend>Select a JPEG or PNG
image of 512KB or smaller to be
uploaded:</legend>
<p><b>File:</b> <input type="file"
name="filename" /></p>
<p>
<label for="caption">Caption:</label>
<input type="text" name="caption" id="caption">
</p>
<p><b>File2:</b> <input type="file"
name="filename2" /></p>
<p>
<label for="caption2">Caption2:</label>
<input type="text" name="caption" id="caption">
</p>
</fieldset>
<div align="center"><input type="submit"
name="submit" value="Submit" /></div>
<input type="hidden" name="submitted"
value="TRUE" />
</form>
PHP
<?php # Upload image
// Check if the form has been submitted:
if (isset($_POST['submitted'])) {
// Check for an uploaded file:
if (isset($_FILES['filename'])) {
// Validate the type. Should be JPEG or PNG.
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPEG', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['filename']['type'], $allowed)) {
// Move the file over.
if (move_uploaded_file ($_FILES['filename']['tmp_name'], "../images/{$_FILES['filename']['name']}")) {
echo '<p><em>The file has been uploaded!</em></p>';
} // End of move... IF.
} else { // Invalid type.
echo '<p class="error">Please upload a JPEG or PNG image.</p>';
}
} // End of isset($_FILES['upload']) IF.
@$path = $_FILES["filename"]["name"];
@$path = mysql_real_escape_string($path);
@$type = $_FILES["filename"]["type"];
@$size = $_FILES["filename"]["size"];
@$nazwa = $_POST["caption"];
$query = "INSERT INTO photographs (";
$query .= " filename, type, size, caption,";
$query .= ") VALUES (";
$query .= " '{$path}', '{$type}', '{$size}', '{$caption}'";
$query .= ")";
echo $query;
$result = mysqli_query($connection, $query);
if ($result) {
// Success
echo " Success. ";
} else {
// Failure
die(" Failure. " . mysqli_error($connection));
// Check for an error:
if ($_FILES['filename']['error'] > 0) {
echo '<p class="error">The file could not be uploaded because: <strong>';
// Print a message based upon the error.
switch ($_FILES['filename']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded.';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write to the disk.';
break;
case 8:
print 'File upload stopped.';
break;
default:
print 'A system error occurred.';
break;
} // End of switch.
print '</strong></p>';
} // End of error IF.
// Delete the file if it still exists:
if (file_exists ($_FILES['filename']['tmp_name']) && is_file($_FILES['filename']['tmp_name']) ) {
unlink ($_FILES['filename']['tmp_name']);
} // End of the submitted conditional.
}
}
?>
答案 0 :(得分:0)
您只使用$filename
执行操作,但您还拥有$filename2
检查$ filename2是否也存在并执行代码。
更好的方法是这样做:
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" value="Send files" />
</form>
每个字段名称都是$ _FILES数组中的键。