一次上载多个文件php html
有人能帮我使我的代码更简单有效吗?我只是个新手。我试图一次上载多个文件,但操作只需一次上载。请修改我的密码一次上载多个文件php html,php,html,Php,Html,有人能帮我使我的代码更简单有效吗?我只是个新手。我试图一次上载多个文件,但操作只需一次上载。请修改我的密码 <input type="file" name="photo" id="fileSelect" multiple="multiple"><?php echo $photo_err;?><br><br> <input type="file" name="photo2" id="fileSelect" multiple="multiple"
<input type="file" name="photo" id="fileSelect" multiple="multiple"><?php echo $photo_err;?><br><br>
<input type="file" name="photo2" id="fileSelect" multiple="multiple"><?php echo $photo_err;?><br><br>
if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0){
$allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename = $_FILES["photo"]["name"];
$filetype = $_FILES["photo"]["type"];
$filesize = $_FILES["photo"]["size"];
$temp = explode(".", $_FILES["photo"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
// Verify file extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!array_key_exists($ext, $allowed))
die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize = 5 * 1024 * 1024;
if($filesize > $maxsize)
die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype, $allowed)){
move_uploaded_file($_FILES["photo"]["tmp_name"], "upload/" . $newfilename);
} else{
echo "Error: There was a problem uploading your file. Please try again.";
}
}
// No file was selected for upload, your (re)action goes here
if($_FILES['photo']['size'] == 0)
{
$photo_err = "Please Select an Image.";
}
任何帮助都将不胜感激。Thx我想这就是你要找的
<?php
define( 'MAX_ALLOWED_FILE_SIZE', 5 * 1024 * 1024 ); // 5Mo
define( 'UPLOAD_PATH', 'upload' );
function is_allowed_file_ext( $filename = null )
{
$allowed_ext = array("jpg", "jpeg", "gif", "png");
$_parts = explode( '.', $filename );
$_ext = end( $_parts );
$_ext = strtolower( $_ext );
if( in_array( $_ext , $allowed_ext ) ){
return $_ext;
}
return false;
}
if( !empty( $_FILES['photo'] ) ){
foreach ($_FILES['photo']['name'] as $key => $filename ) {
if( $_FILES['photo']['error'][$key] == UPLOAD_ERR_OK ){
if( $file_ext = is_allowed_file_ext( $filename ) ){
$new_filename = sprintf( '%d-%s.%s', round(microtime(true)), uniqid(), $file_ext );
if( $_FILES['photo']['size'][$key] <= MAX_ALLOWED_FILE_SIZE ){
// Everything is okay now, save the file
$dest_path = sprintf( '%s/%s', rtrim(UPLOAD_PATH,'/'), $new_filename );
move_uploaded_file( $_FILES['photo']['tmp_name'][$key], $dest_path );
}else{
$photo_errors[] = 'Error: File size is larger than the allowed limit.';
}
}else{
$photo_errors[] = $filename . ' is not a valid image';
}
}else{
switch ( $_FILES['photo']['error'][$key] ) {
case UPLOAD_ERR_NO_FILE:
$photo_errors[] = 'No file sent';
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$photo_errors[] = 'Max file size exceeded!';
break;
default:
$photo_errors[] = 'Unknown errors';
break;
}
}
}
}else{
$photo_errors[] = 'No photos selected';
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="photo[]" id="fileSelect" multiple="multiple">
<?php foreach ($photo_errors as $photo_error):?>
<div class="photo-error"><?php echo htmlspecialchars( $photo_error ); ?></div>
<?php endforeach; ?>
<button type="submit">Submit</button>
</form>
提交
您应该阅读有关多次上传的手册:顺便说一句,两个字段的id相同。这是无效的。id在文档中必须是唯一的。此代码工作正常。谢谢你的帮助如果你能再帮我一次,我如何分离$new_文件名,这样我就可以把它放在不同列的数据库中。谢谢
<?php
define( 'MAX_ALLOWED_FILE_SIZE', 5 * 1024 * 1024 ); // 5Mo
define( 'UPLOAD_PATH', 'upload' );
function is_allowed_file_ext( $filename = null )
{
$allowed_ext = array("jpg", "jpeg", "gif", "png");
$_parts = explode( '.', $filename );
$_ext = end( $_parts );
$_ext = strtolower( $_ext );
if( in_array( $_ext , $allowed_ext ) ){
return $_ext;
}
return false;
}
if( !empty( $_FILES['photo'] ) ){
foreach ($_FILES['photo']['name'] as $key => $filename ) {
if( $_FILES['photo']['error'][$key] == UPLOAD_ERR_OK ){
if( $file_ext = is_allowed_file_ext( $filename ) ){
$new_filename = sprintf( '%d-%s.%s', round(microtime(true)), uniqid(), $file_ext );
if( $_FILES['photo']['size'][$key] <= MAX_ALLOWED_FILE_SIZE ){
// Everything is okay now, save the file
$dest_path = sprintf( '%s/%s', rtrim(UPLOAD_PATH,'/'), $new_filename );
move_uploaded_file( $_FILES['photo']['tmp_name'][$key], $dest_path );
}else{
$photo_errors[] = 'Error: File size is larger than the allowed limit.';
}
}else{
$photo_errors[] = $filename . ' is not a valid image';
}
}else{
switch ( $_FILES['photo']['error'][$key] ) {
case UPLOAD_ERR_NO_FILE:
$photo_errors[] = 'No file sent';
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$photo_errors[] = 'Max file size exceeded!';
break;
default:
$photo_errors[] = 'Unknown errors';
break;
}
}
}
}else{
$photo_errors[] = 'No photos selected';
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="photo[]" id="fileSelect" multiple="multiple">
<?php foreach ($photo_errors as $photo_error):?>
<div class="photo-error"><?php echo htmlspecialchars( $photo_error ); ?></div>
<?php endforeach; ?>
<button type="submit">Submit</button>
</form>