dqgo99177 2016-06-04 23:09
浏览 59

文件未上传

I'm on linux, running xampp. index.php

    <form class="well" action="upload.php" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="file">Select a file to upload</label>
            <input type="file" name="file">
            <p class="help-block">Only jpg, jpeg, png, wav, mp3, wav and mp4 files are allowed.</p>
        </div>
        <input type="submit" class="btn btn-lg btn-primary" value="Upload">
    </form>

upload.php

<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

        $name     = $_FILES['file']['name'];
        $tmpName  = $_FILES['file']['tmp_name'];
        $error    = $_FILES['file']['error'];
        $size     = $_FILES['file']['size'];
        $ext      = strtolower(pathinfo($name, PATHINFO_EXTENSION));

        switch ($error) {
            case UPLOAD_ERR_OK:
                $valid = true;
                //validate file extensions
                if ( !in_array($ext, array('jpg','jpeg','png','mp3', 'mp4', 'wav', 'webm')) ) {
                    $valid = false;
                    $response = 'Invalid file extension.';
                }
                //validate file size
                /*if ( $size/1024/1024 > 2 ) {
                    $valid = false;
                    $response = 'File size is exceeding maximum allowed size.';
                }*/
                //upload file
                if ($valid) {
                    $targetPath =  'uploads/';

                    move_uploaded_file($tmpName,$targetPath);
                    header( 'Location: index.php' ) ;
                    exit;
                }
                break;
            case UPLOAD_ERR_INI_SIZE:
                $response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
                break;
            case UPLOAD_ERR_FORM_SIZE:
                $response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
                break;
            case UPLOAD_ERR_PARTIAL:
                $response = 'The uploaded file was only partially uploaded.';
                break;
            case UPLOAD_ERR_NO_FILE:
                $response = 'No file was uploaded.';
                break;
            case UPLOAD_ERR_NO_TMP_DIR:
                $response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
                break;
            case UPLOAD_ERR_CANT_WRITE:
                $response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
                break;
            case UPLOAD_ERR_EXTENSION:
                $response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
                break;
            default:
                $response = 'Unknown error';
            break;
        }

        echo $response;
    }

    ?>

Phpinfo: file upload on, Max file size 128M. uploads folder is empty, it's in the root directory where the script is located, no errors are displayed and nothing seems wrong with the script

  • 写回答

2条回答 默认 最新

  • doufang8965 2016-06-04 23:18
    关注

    So you kinda need to get to the folder you are trying to access. I had the same problem i think a while back. The solution i found was doing this with the $targetpath:

    You need to get the webserver path, in order to do this you use the dirname(__FILE__) function. Depending on how far you are going to go in the file path. Forexample i needed to access a folder "nydemo1/bilder/images/folders/" then i needed this for my filepath variable:

    dirname(dirname(dirname(__FILE__)) //because this is 3 folders deep

    Hope this helps

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据