首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >上传图像PHP SQL

上传图像PHP SQL
EN

Stack Overflow用户
提问于 2018-08-19 03:46:13
回答 1查看 534关注 0票数 0

我的上传脚本有问题,希望有人能帮助我。

代码语言:javascript
复制
<?php
if(isset($_POST['submit'])){
    // Include the database configuration file
    include_once 'dbconfig.php';

    // File upload configuration
    $targetDir = "uploads/";
    $allowTypes = array('jpg','png','jpeg','gif');


    $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
    if(!empty(array_filter($_FILES['files']['name']))){
        foreach($_FILES['files']['name'] as $key=>$val){
            // File upload path
            $fileName = basename($_FILES['files']['name'][$key]);
            $targetFilePath = $targetDir . $fileName;

            // Check whether file type is valid
            $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
            if(in_array($fileType, $allowTypes)){
                // Upload file to server
                if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){
                    // Image db insert sql
                    $insertValuesSQL .= "('".$fileName."', NOW()),";

                }else{
                    $errorUpload .= $_FILES['files']['name'][$key].', ';

                }
            }else{
                $errorUploadType .= $_FILES['files']['name'][$key].', ';
            }
        }

        if(!empty($insertValuesSQL)){
            $insertValuesSQL = trim($insertValuesSQL,',');
            // Insert image file name into database
            $insert = $db->query("INSERT INTO images (file_name, uploaded_on) VALUES $insertValuesSQL");
            if($insert){
                $errorUpload = !empty($errorUpload)?'Upload Error: '.$errorUpload:'';
                $errorUploadType = !empty($errorUploadType)?'File Type Error: '.$errorUploadType:'';
                $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType;
                $statusMsg = "Files are uploaded successfully.".$errorMsg;
            }else{
                $statusMsg = "Sorry, there was an error uploading your file.";
            }
        }

    }else{
        $statusMsg = 'Please select a file to upload.';
    }

    // Display status message
    echo $statusMsg;

}
?>

<html>
<form action="" method="post" enctype="multipart/form-data">
    Select Image Files to Upload:
    <input type="file" name="files[]" multiple>

    <input class="upload-btn" type="submit" name="submit" value="UPLOAD">
</form>
</html>

如果我单击submit按钮,但没有选择任何图像,则代码将在屏幕上打印Please select a file to upload.

如果我用选中的图像单击submit按钮,代码不会显示任何内容,也不会上传任何图像。

出什么问题了?

EN

回答 1

Stack Overflow用户

发布于 2018-08-19 08:06:00

似乎未正确访问文件数组。试试这段代码

代码语言:javascript
复制
if(!empty($_FILES)){
    foreach($_FILES as $key=>$val){
        // File upload path
        $fileName = basename($val['name']);
        $targetFilePath = $targetDir . $fileName;

        // Check whether file type is valid
        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
        if(in_array($fileType, $allowTypes)){
            // Upload file to server
            if(move_uploaded_file($val["tmp_name"], $targetFilePath)){
                // Image db insert sql
                $insertValuesSQL .= "('".$fileName."', NOW()),";

            }else{
                $errorUpload .= $val['name'].', ';

            }
        }else{
            $errorUploadType .= $val['name'].', ';
        }
    }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51912042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档