duanrenzou1619 2013-10-06 19:07
浏览 31
已采纳

PHP上传多个图像并检查其文件扩展名

I have the following code to upload a file to a folder, depending on the file input upload the file is renamed e.g. the first input - the picture is renamed to picture01.jpg, the 5th input - the picture is renamed to picture to picture01.jpg, etc...

<form action="upload_file.php" enctype="multipart/form-data" method="POST">
    <p><input name="image01" type="file"> </p>
    <p><input name="image02" type="file"> </p>
    <p><input name="image03" type="file"> </p>
    <p><input name="image04" type="file"> </p>
    <p><input name="image05" type="file"> </p>
    <input type="submit" value="Upload File">
</form>

This is upload_file.php -

<?php 

$pic_names = array (
    '01' => 'picture01',
    '02' => 'picture02',
    '03' => 'picture03',
    '04' => 'picture04',
    '05' => 'picture05',
                    );


$folder = "temp_images/"; 

if(preg_match("/(jpg|jpeg|png|gif|mp3|mp4)/",$ext)) {
// Picture01
$image_path01 = $folder . $pic_names['01'] . '.jpg';
move_uploaded_file($_FILES['image01']['tmp_name'], $image_path01);
// Picture02
$image_path02 = $folder . $pic_names['02'] . '.jpg';
move_uploaded_file($_FILES['image02']['tmp_name'], $image_path02);
// Picture03
$image_path03 = $folder . $pic_names['03'] . '.jpg';
move_uploaded_file($_FILES['image03']['tmp_name'], $image_path03);
// Picture04
$image_path04 = $folder . $pic_names['04'] . '.jpg';
move_uploaded_file($_FILES['image04']['tmp_name'], $image_path04);
// Picture05
$image_path05 = $folder . $pic_names['05'] . '.jpg';
move_uploaded_file($_FILES['image05']['tmp_name'], $image_path05);

    echo 'Uploaded successfully!';
   } else { echo 'Uploaded successfully!';}
    ?>

I would like to check that before the file is uploaded it is checked that either one of the following extensions exist prior to uploading jpg, gif or png. If they have another extension then it is not uploaded and an error is given.

The only solution I managed to find is when the input name="image" type="file"> have the same name e.g. in this case name. How can I do it for my case with different names for the input?

I got the follwoing error: PHP Parse error: syntax error, unexpected T_ELSE

Though I am sure that it more than a parse error.

Any suggestion or help please?

UPDATE: I amended the code but its telling me that 'Wrong file type!' for any image format too.

  • 写回答

2条回答 默认 最新

  • dongshi9407 2013-10-06 21:04
    关注

    The error is in both string echo 'Uploaded successfully!';{ position and content.

    1) No any statements is allowed between } and else.

    2) 'Uploaded successfully!';{ contains { which is superfluous.

    Update: One of a quite good ways to process uploaded files. Depends on $pic_names array from OP.

    $pic_names = array (
        '01' => 'picture01',
        '02' => 'picture02',
        '03' => 'picture03',
        '04' => 'picture04',
        '05' => 'picture05',
    );
    
    $allowed_mime = array(
        'image/jpeg' => 'jpg',
        'image/pjpeg' => 'jpg',
        'image/gif' => 'gif',
        'image/png' => 'png',
    );
    
    $upload_messages = array(
        UPLOAD_ERR_OK => ' Uploaded successfully!',
        UPLOAD_ERR_INI_SIZE => ' The uploaded file exceeds the maxumum size of ' . ini_get('upload_max_filesize') . '.',
        UPLOAD_ERR_PARTIAL => ' The uploaded file was only partially uploaded.',
        UPLOAD_ERR_NO_FILE => ' No file was uploaded',
        UPLOAD_ERR_NO_TMP_DIR => ' Missing a temporary folder.',
        UPLOAD_ERR_CANT_WRITE => ' Failed to write file to disk.',
        UPLOAD_ERR_EXTENSION => ' A PHP extension stopped the file upload.',
        100 => ' Wrong file type.',
    );
    
    $folder = "temp_images/"; 
    
    $error_message = $success_message = '';
    $upload_counter = 0;
    
    foreach ($pic_names as $key => $value) {
        if (isset($_FILES['image'.$key]) && !$_FILES['image'.$key]['error']) {
            $file_mime = getFileMime($_FILES['image'.$key]['tmp_name']);
    
            if ( in_array($file_mime, array_keys($allowed_mime)) ) {
                $new_path = $folder . $value . '.' . $allowed_mime[$file_mime];
    
                if ( !move_uploaded_file($_FILES['image'.$key]['tmp_name'], $new_path) ) {
                    $error_message .= ' Image' . $key . ':' . $upload_messages[UPLOAD_ERR_CANT_WRITE];
                } else {
                    $upload_counter++;
                }
            } else {
                $error_message .= ' Image' . $key . ':' . $upload_messages[100];
            }
        } else {
            $error_message .= ' Image' . $key . ':' . ($_FILES['image'.$key]['error'] ? $upload_messages[$_FILES['image'.$key]['error']] : $upload_messages[UPLOAD_ERR_NO_FILE]);
        }
    }
    
    if ( $upload_counter == count($pic_names) ) {
        echo $upload_messages[UPLOAD_ERR_OK];
    } else {
        echo 'Loaded ' . $upload_counter . ' file' . ($upload_counter != 1 ? 's' : '') . '.' . $error_message;
    }
    
    function getFileMime ($file) {
        if (PHP_VERSION >= '5.3.0') {
            $finfo = new finfo(FILEINFO_MIME_TYPE);
            return $finfo->file($file);
        } else {
            return mime_content_type($file);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错