PHP文件上传返回错误

时间:2013-10-22 08:57:04

标签: php html5 file-upload

我试图让以下php文件上传脚本工作,我已经在所有变量上使用了var_dump()和print_r,据我所知它应该可行。事实上这是有效的...在我的Windows安装上,我目前正在运行Ubuntu 12.04,由于某种原因,这将无法在我的本地设置上工作,我不愿意尝试它,这可能是我的/ tmp /的问题权限?

<?php
    function upload() {
        /*     * * check if a file was uploaded ** */
        if (is_uploaded_file($_FILES['images']['tmp_name']) && getimagesize($_FILES['images']['tmp_name']) != false) {
            /*         * *  get the image info. ** */
            $size = getimagesize($_FILES['images']['tmp_name']);
            /*         * * assign our variables ** */
            $type = $size['mime'];
            $imgfp = fopen($_FILES['images']['tmp_name'], 'rb');
            $size = $size[3];
            $name = $_FILES['images']['name'];
            $maxsize = 99999999;
            $target_path = "/uploads/";
            /* Add the original filename to our target path.  
              Result is "uploads/filename.extension" */
            $currentDate = date("Y-m-d");
            $target_path = $target_path . $currentDate . ' - ' . basename($_FILES['images']['name']);
            /*         * *  check the file is less than the maximum file size ** */
            if ($_FILES['images']['size'] < $maxsize) {
                                if (move_uploaded_file($_FILES['images']['tmp_name'], $target_path)) {
                    echo "The file " . basename($_FILES['images']['name']) .
                    " has been uploaded";

                } else {
                    echo "<div class='error'>There was an error uploading the file, please <a href='../artworkGenerator'>try again!</a></div>";
                }
            } else {
                /*             * * throw an exception if image is not of type ** */
                throw new Exception("File Size Error");
            }
        } else {
            // if the file is not less than the maximum allowed, print an error
            throw new Exception("Unsupported Image Format!");
        }
        print_r();
    }

    /* * * check if a file was submitted ** */
    if (!isset($_FILES['images'])) {
        echo '<p>Please select a file</p>';
    } else {
        try {
            upload();
            /*         * * give praise and thanks to the php gods ** */
            echo '<p>Thank you for submitting</p>';
        } catch (Exception $e) {
            echo '<h4>' . $e->getMessage() . '</h4>';
        }
    }
    ?>

上传表单:

<form id="uploadForm" enctype="multipart/form-data" action="upload.php" method="POST">  


                                <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
                                <label>Attach file<span class="required"> *</span></label><img style="display:none;float:right;position: relative;top: 45px;right: 5px;" class="logo" src="img/tick.png" alt="logo" />
                                <hr class="small"/>
                                <br/>
                                <input type="file" name="images" id="images" multiple />
                                <ul id="image-list">  
                                </ul> 
                                <br/><br/>
                           <hr style="clear:both">
                        <input class="btn btn-success save" type="submit" id="btn" value="Generate"/>

                        <div id="invisible" style="display:none;">
                        </div>
                    </form>

1 个答案:

答案 0 :(得分:0)

您是否尝试过相对于执行此操作的php文件的上传路径?

也可能是权限错误,所以要快速检查chmod 777你试图保存的文件夹