简单的文件上传脚本

时间:2012-02-09 08:15:38

标签: php file-upload

我编写了一个简单的文件上传脚本,但它给出了未定义索引file1的错误。

<html>
    <body>
        <form method="post">
            <label for="file">Filename:</label>
            <input type="file" name="file1" id="file1" /> 
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>
<?php
if(isset($_POST['submit'])) {
    if ($_FILES["file1"]["error"] > 0) {
        echo "Error: " . $_FILES["file1"]["error"] . "<br />";
    } else {
        echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
        echo "Type: " . $_FILES["file1"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " Kb<br />";
        echo "Stored in: " . $_FILES["file1"]["tmp_name"];
    }
}
?>

代码中有什么问题?

7 个答案:

答案 0 :(得分:13)

您的enctype="multipart/form-data"元素中缺少<form>

答案 1 :(得分:3)

简单的php文件上传脚本的另一个解决方案是: (制作一个yourfile.php并插入以下代码。然后将yourfile.php放在您的网站上)

<?php
$pass = "YOUR_PASSWORD";
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1256" /></head><body>
<?php
if (!empty($_GET['action']) &&  $_GET['action'] == "logout") {session_destroy();unset ($_SESSION['pass']);}

$path_name = pathinfo($_SERVER['PHP_SELF']);
$this_script = $path_name['basename'];
if (empty($_SESSION['pass'])) {$_SESSION['pass']='';}
if (empty($_POST['pass'])) {$_POST['pass']='';}
if ( $_SESSION['pass']!== $pass) 
{
    if ($_POST['pass'] == $pass) {$_SESSION['pass'] = $pass; }
    else 
    {
        echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"><input name="pass" type="password"><input type="submit"></form>';
        exit;
    }
}
?>


<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" /></form>


<?php 

if (!empty($_FILES["file"]))
{
    if ($_FILES["file"]["error"] > 0)
       {echo "Error: " . $_FILES["file"]["error"] . "<br>";}
    else
       {echo "Stored file:".$_FILES["file"]["name"]."<br/>Size:".($_FILES["file"]["size"]/1024)." kB<br/>";
       move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
       }
}

    // open this directory 
    $myDirectory = opendir(".");
    // get each entry
    while($entryName = readdir($myDirectory)) {$dirArray[] = $entryName;} closedir($myDirectory);
    $indexCount = count($dirArray);
        echo "$indexCount files<br/>";
    sort($dirArray);

    echo "<TABLE border=1 cellpadding=5 cellspacing=0 class=whitelinks><TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n";

        for($index=0; $index < $indexCount; $index++) 
        {
            if (substr("$dirArray[$index]", 0, 1) != ".")
            {
            echo "<TR>
            <td><a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>
            <td>".filetype($dirArray[$index])."</td>
            <td>".filesize($dirArray[$index])."</td>
                </TR>";
            }
        }
    echo "</TABLE>";
    ?>

答案 2 :(得分:2)

进行以下更改并尝试。

<form method="post" action="" enctype="multipart/form-data" >

答案 3 :(得分:1)

HTML

 <!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

答案 4 :(得分:0)

void Star::makeStar(){
    // every if statement covers Star with n and multiples of n vertexes
    // the inner for loops execute one iteration for the fundamental stars
    // and n iterations for the multiples (rotational symmetry)
    if (vertexNumber % 5 == 0){
        for (size_t it = 0, polygons = starCoordinates.size() / 5; it < polygons; ++it){
            Lines::add(starCoordinates[it + polygons * 3], starCoordinates[it]);
            Lines::add(starCoordinates[it], starCoordinates[it + polygons * 2]);
            Lines::add(starCoordinates[it + polygons * 2], starCoordinates[it + polygons * 4]);
            Lines::add(starCoordinates[it + polygons * 4], starCoordinates[it + polygons * 1]);
            Lines::add(starCoordinates[it + polygons * 1], starCoordinates[it + polygons * 3]);
        }
    }else if (vertexNumber % 6 == 0){ // for polygons multiples of 6
        size_t polygons = starCoordinates.size() / 6;
        for (size_t it = 0, ; it < starCoordinates() / 3; ++it){
            // generated by two triangles 
            Lines::add(starCoordinates[it], starCoordinates[it + polygons * 2]);
            Lines::add(starCoordinates[it + polygons * 2], starCoordinates[it + polygons * 4]);
            Lines::add(starCoordinates[it + polygons * 4], starCoordinates[it]);
        }
    }else if (vertexNumber % 7 == 0){
        for (size_t it = 0, polygons = starCoordinates.size() / 7; it < polygons; ++it){
            Lines::add(starCoordinates[it], starCoordinates[it + polygons * 3]);
            Lines::add(starCoordinates[it + polygons * 3], starCoordinates[it + polygons * 6]);
            Lines::add(starCoordinates[it + polygons * 6], starCoordinates[it + polygons * 2]);
            Lines::add(starCoordinates[it + polygons * 2], starCoordinates[it + polygons * 5]);
            Lines::add(starCoordinates[it + polygons * 5], starCoordinates[it + polygons * 1]);
            Lines::add(starCoordinates[it + polygons * 1], starCoordinates[it + polygons * 4]);
            Lines::add(starCoordinates[it + polygons * 4], starCoordinates[it]);
        }
    }else if (vertexNumber % 8 == 0){
        for (size_t it = 0, polygons = starCoordinates.size() / 8; it < polygons; ++it){
            Lines::add(starCoordinates[it], starCoordinates[it + polygons * 5]);
            Lines::add(starCoordinates[it + polygons * 5], starCoordinates[it + polygons * 2]);
            Lines::add(starCoordinates[it + polygons * 2], starCoordinates[it + polygons * 7]);
            Lines::add(starCoordinates[it + polygons * 7], starCoordinates[it + polygons * 4]);
            Lines::add(starCoordinates[it + polygons * 4], starCoordinates[it + polygons * 1]);
            Lines::add(starCoordinates[it + polygons * 1], starCoordinates[it + polygons * 6]);
            Lines::add(starCoordinates[it + polygons * 6], starCoordinates[it + polygons * 3]);
            Lines::add(starCoordinates[it + polygons * 3], starCoordinates[it]);
       }
   } else throw std::invalid_argument("Star vertexes'number not supported!");
}

答案 5 :(得分:0)

进行以下更改,以下网站

$(document).ready(function(){
    $('input[teramfile-kname=teramfile_formajax]').click(function(e){
        $('.main aside[class=upload]').fadeOut('slow');
        $('.main aside[class=startupload]').delay(800).fadeIn('slow');
        e.preventDefault();
        var formData = new FormData($(this).parents('form[teramfile-fname='+$('input[teramfile-kname=teramfile_formajax]').attr('teramfile-fname')+']')[0]);
        $.ajax({
            url: $('form[teramfile-fname='+$('input[teramfile-kname=teramfile_formajax]').attr('teramfile-fname')+']').attr('action'),
            type: $('form[teramfile-fname='+$('input[teramfile-kname=teramfile_formajax]').attr('teramfile-fname')+']').attr('method'),
            xhr: function() {
                var myXhr = $.ajaxSettings.xhr();
                return myXhr;
                return false; 
            },
            success: function (data) {
                $('.main aside[class=startupload]').fadeOut('speed');  
                $('.main aside[class=resultupload]').delay(1000).html(data).fadeIn('slow');
                $('.main aside[class=newupload]').delay(1000).fadeIn();
            },
            data: formData,
            cache: false,
            contentType: false,
            processData: false
        });
        return false;
    });
});

http://www.teramfile.com/filedetails/e4fd5b2ec2/

答案 6 :(得分:-1)

主要问题是您的表单没有通过http发送文件内容的选项。 要从输入元素发送二进制数据和文本数据,您需要在表单标记中添加额外的属性。

<form method="post" enctype="multipart/form-data">

然后在PHP代码中

试试这一行

<?php

print_r($_FILES);

?>

上面的代码将显示有关从表单上传文件的所有信息。