我正在尝试列出通过表单上传的图片的所有文件名,然后通过电子邮件发送。
fileupload.html:
<form method="post" action="upload.php" enctype="multipart/form-data" id="upload" >
<input type="file" name="filesupl[]" multiple id="files" />
<input type="submit" name="UploadBtn" value="Upload" class="formtext" id="UploadBtn">
</form>
upload.php的:
$art_file = $_FILES['filesupl'];
$filecounter = 1;
$count = 0;
foreach ( $art_file as $i => $art_inner ){
echo $filecounter . ") " . $art_file[$i]['name'] . "<br />"; $filecounter++;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['filesupl']))
{
$extension = pathinfo($_FILES['filesupl']['name'], PATHINFO_EXTENSION);
// Upload files
// loop all files
foreach ( $_FILES['filesupl']['name'] as $i => $name )
{
// if file not uploaded then skip it
if ( !is_uploaded_file($_FILES['filesupl']['tmp_name'][$i]) )
continue;
// skip large files
if ( $_FILES['filesupl']['size'][$i] >= $max_size )
continue;
// skip unprotected files
if( !in_array(pathinfo($name, PATHINFO_EXTENSION), $extensions) )
continue;
// now we can move uploaded files
if( move_uploaded_file($_FILES["filesupl"]["tmp_name"][$i], $dir . $name) )
$count++;
}
}
echo json_encode(array('count' => $count));
我尝试了多种方法来访问上传文件的“名称”(上面的代码就是其中一种尝试),但无济于事。
答案 0 :(得分:0)
我试图列出上传图片的所有文件名...
如果您的唯一目的是获取上传文件的所有文件名,那么您的代码应该是这样的:
foreach($_FILES['filesupl']['name'] as $name){
// echo $name . '<br />';
}
现在,如果您想重构代码并使其正常工作,请使用for
这样的循环:
$count = count($_FILES['filesupl']['name']);
for($i = 0; $i < $count; ++$i){
// if file not uploaded then skip it
if ( !is_uploaded_file($_FILES['filesupl']['tmp_name'][$i]) )
continue;
// skip large files
if ( $_FILES['filesupl']['size'][$i] >= $max_size )
continue;
// get file extension
$extension = pathinfo($_FILES['filesupl']['name'][$i], PATHINFO_EXTENSION);
// skip unprotected files
if( !in_array($extension, $extensions) )
continue;
// now we can move uploaded files
if( move_uploaded_file($_FILES["filesupl"]["tmp_name"][$i], $dir . $_FILES['filesupl']['name'][$i])){
// file has been uploaded
}
}
echo json_encode(array('count' => $count));