有人可以帮助我让我的代码更简单有效吗? srry我只是一个新手。我尝试一次上传多个文件,但操作只需一次上传。请修复我的代码thx
<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.";
}
另外
if(isset($_FILES["photo2"]) && $_FILES["photo2"]["error"] == 0){
$allowed2 = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
$filename2 = $_FILES["photo2"]["name"];
$filetype2 = $_FILES["photo2"]["type"];
$filesize2 = $_FILES["photo2"]["size"];
$temp2 = explode(".", $_FILES["photo2"]["name"]);
$newfilename2 = round(microtime(true)) . '.' . end($temp2);
// Verify file extension
$ext2 = pathinfo($filename2, PATHINFO_EXTENSION);
if(!array_key_exists($ext2, $allowed2))
die("Error: Please select a valid file format.");
// Verify file size - 5MB maximum
$maxsize2 = 5 * 1024 * 1024;
if($filesize2 > $maxsize2)
die("Error: File size is larger than the allowed limit.");
// Verify MYME type of the file
if(in_array($filetype2, $allowed2)){
move_uploaded_file($_FILES["photo2"]["tmp_name"], "upload/" . $newfilename2);
} 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['photo2']['size'] == 0)
{
$photo_err2 = "Please Select an Image.";
}
任何帮助都会感激不尽.Thx
答案 0 :(得分:0)
我认为这就是你要找的东西
<?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>