gpt4 book ai didi

php - 多张照片上传php mysql

转载 作者:行者123 更新时间:2023-11-30 01:13:01 25 4
gpt4 key购买 nike

这是上传带有描述的文件的代码。文件传输工作没有问题。我不知道如何使用它上传多个文件。我想发送 2 个文件。我尝试了很多方法来解决自己的问题,但我已经全力以赴:)提前感谢您的帮助。 enter image description here

表单

    <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.
}
}

?>

最佳答案

你只用$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 数组中的一个键。

关于php - 多张照片上传php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19313021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com