[英]How to upload files from two html input fields in the same form using php?
Hi I am facing a problem while uploading two files using php.嗨,我在使用 php 上传两个文件时遇到问题。 I have this html input form with two files field我有这个带有两个文件字段的 html 输入表单
<form class="form-group" method="post" enctype="multipart/form-data">
<input type="file" accept=".jpg, .jpeg, .png" id="img" name="displaypic" required/>
<input type="file" accept=".pptx" name="presentation" required>
<button name="submit>Submit</submit>
</form>
This is my php code.这是我的php代码。 Here I take the file data from the form but only the first one is uploaded, second file is not.在这里,我从表单中获取文件数据,但只上传了第一个文件,没有上传第二个文件。
<?php
if(isset($_POST['submit'])){
$file = $_FILES['displaypic'];
$fileName = $_FILES['displaypic']['name'];
$tempName = $_FILES['displaypic']['tmp_name'];
$size = $_FILES['displaypic']['size'];
$error = $_FILES['displaypic']['error'];
$format = $_FILES['displaypic']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg','png');
if(in_array($fileActualExt, $allowed)) {
if ($error === 0) {
if ($size<2e6) {
$newname = $tid.".".$fileActualExt;
$location = 'displays/'.$newname;
move_uploaded_file($tempName,$location);
}}}
Similarly when I write the same code for file two it doesn't work.同样,当我为文件 2 编写相同的代码时,它不起作用。 Only the first file is uploaded not the second file.仅上传第一个文件,而不上传第二个文件。
$file_ppt = $_FILES['presentation'];
$fileName = $_FILES['presentation']['name'];
$tempName = $_FILES['presentation']['tmp_name'];
$size = $_FILES['presentation']['size'];
$error = $_FILES['presentation']['error'];
$format = $_FILES['presentation']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('pptx');
if(in_array($fileActualExt, $allowed)) {
if ($error === 0) {
if ($size<10e6) {
$newname = $tid.".".$fileActualExt;
$location = 'presentations/'.$newname;
move_uploaded_file($tempName,$location);
}}}
} ?> } ?>
Kindly guide me and help me in this code.请指导我并帮助我完成此代码。
If you use the same name for the file input field but use the array style syntax for the name you can assign your own identifier within the square braces which will be available in the POST / FILES array later.如果您对文件输入字段使用相同的名称,但对名称使用数组样式语法,您可以在方括号内分配您自己的标识符,稍后将在 POST / FILES 数组中提供该标识符。 This identifier can be used to separate the different types of files so you can fork the logic as appropriate to your needs.此标识符可用于分隔不同类型的文件,以便您可以根据需要分叉逻辑。
The following shows a basic usage of this methodology - it might prove of interest but it might not.下面显示了这种方法的基本用法 - 它可能会被证明很有趣,但也可能不会。
<?php
$field='xfiles'; // Whatever you wish to name your file input elements
$errors=array();
$status=array();
$maxfs=pow(1024,2) * 5; //5Mb or whatever.... 10e6?
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES[ $field ] ) ){
$obj=$_FILES[ $field ];
foreach( $obj['name'] as $index => $void ){
$name=$obj['name'][ $index ];
$tmp=$obj['tmp_name'][ $index ];
$error=$obj['error'][ $index ];
$type=$obj['type'][ $index ];
$size=$obj['size'][ $index ];
$ext=strtolower(pathinfo($name,PATHINFO_EXTENSION));
$allowed=(object)array(
'displaypic' => array('jpg','jpeg','png'),
'presentation' => array('ppt','pptx')
);
if( $error!==UPLOAD_ERR_OK )$errors[]=sprintf('An error [code:%d] occurred with file %s',$error,$name);
if( !in_array( $ext, $allowed->$index ) )$errors[]=sprintf('Incorrect file extension %s for %s',$ext,$name);
if( $size > $maxfs )$errors[]=sprintf('The file %s is too large @%d',$name,$size);
if( empty( $errors ) ){
$status[]=sprintf('<div>%s uploaded successfully - save to db, do a happy little dance or whatever else you need to do!</div>', $name );
#move_uploaded_file($tmp,'/path/to/new/folder/'.$name);
#$sql='insert into ....';
}
}
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<title>PHP: Multiple file uploads</title>
<meta charset='utf-8' />
</head>
<body>
<form class='form-group' method='post' enctype='multipart/form-data'>
<label>Display - [accept:jpg,png]<input type='file' accept='.jpg, .jpeg, .png' name='xfiles[displaypic]' required /></label>
<label>Presentation - [accept:ppt,pptx] <input type='file' accept='.ppt, .pptx' name='xfiles[presentation]' required /></label>
<input type='submit' />
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $status ) ){
echo '<h1>Success</h1>';
foreach($status as $msg)printf('<div>%s</div>',$msg);
}
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $errors ) ){
echo '<h1>Error</h1>';
foreach($errors as $error)printf('<div>%s</div>',$error);
}
?>
</form>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.