简体   繁体   中英

Image - Upload not responding, no access to $_FILES

Here is my file-upload script, and i am getting the following error

Notice: Undefined index: fupload in C:\Users\Tuskar\Desktop\Projekt\htdocs\Project IT-Space\Profile\edit_profile_parse.php on line 8

But according there should not error, because i identified the index. It seems i don't have access to the $_FILES array, because before i got this error ive been getting other similar errors or the programm completely passes the if and goes directly to the else (file not chosen)

I know the script is primitive and includes almost no security, but i just want it to work first before i add other features like max file size or file restriction ... :(

Here is the code i am using.

Upload Picture
<form action="edit_profile_parse.php" method="get" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
<input type="file" name="fupload"> </input>
<input type="submit" name="submit" value="Upload"> </input>
</form>

Here is the php that handles the form

if (isset( $_GET['submit'] ))
{
if (isset($_FILES['fupload'] ))
{
echo "name: ".$_FILES['fupload']['name']." <br> ";
echo "size: ".$_FILES['fupload']['sizw']." <br> ";
echo "type: ".$_FILES['fupload']['type']." <br> ";

    if ($_FILES['fupload']['type'] == "image/gif")
    {
        $source = $_FILES['fupload']['tmp_name'];
        $target = "images/" .$_FILES['fupload']['name'];
        move_uploaded_file($source, $target) or die ("Error: " .mysql_error());
        $size = getImageSize($target);

        $imgstr = "<img src=\" '".$target."' \">";
        echo $imgstr;
    }
    else
    {
    echo "Problem uploading the file ... ";
    }
}   
else
{
echo "No file chosen !! ";
}
}
else
{
echo "Button not clicked ";
}

您应该使用表单方法来发布而不是获取。

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

Make sure your FORM tag has method="POST". GET requests do not support multipart/form-data uploads.

I hope this works: the form:

<form action="edit_profile_parse.php" method="post" enctype="multipart/form-data" >
    <input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
    <input type="file" name="fupload"> </input>
    <input type="submit" name="submit" value="Upload"> </input>
</form>

the php file:

<?php
if($_POST) {
    $max_size = mysql_real_escape_string(strip_tags($_POST['MAX_FILE_SIZE']));
    $file = $_FILES['fupload']['name'];

    if(isset($max_size) && !empty($max_size) && !empty($file)) {
        $file_type = $_FILES['fupload']['type'];
        $tmp = $_FILES['fupload']['tmp_name'];
        $file_size = $_FILES['fupload']['size'];

        $allowed_type = array('image/png', 'image/jpg', 'image/jpeg', 'image/gif');

        if(in_array($file_type, $allowed_type)) {
            if($file_size < $max_size) {
                $path = 'images/'.$file;

                move_uploaded_file($tmp, $path);
                //if you want to store the file in a db use the $path in the query
            } else {
                echo 'File size: '.$file_size.' is too big';
            }
        } else {
            echo 'File type: '.$file_type.' is not allowed';
        }
    } else {
        echo 'There are empty fields';
    }
}
?>

Upload Picture

<form action="edit_profile_parse.php" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="999999999"> </input>
<input type="file" name="fupload"> </input>
<input type="submit" name="submit" value="Upload"> </input>
</form>  

PHP file

 <?php 
    if (isset( $_POST['submit'] ))
    {
    if (isset($_FILES['fupload'] ))
    {
    echo "name: ".$_FILES['fupload']['name']." <br> ";
    echo "size: ".$_FILES['fupload']['size']." <br> ";
    echo "type: ".$_FILES['fupload']['type']." <br> ";

        if ($_FILES['fupload']['type'] == "image/gif")
        {
            $source = $_FILES['fupload']['tmp_name'];
            $target = "images/" .$_FILES['fupload']['name'];
            move_uploaded_file($source, $target) or die ("Error: " .mysql_error());
            $size = getImageSize($target);

            $imgstr = "<img src=\" '".$target."' \">";
            echo $imgstr;
        }
        else
        {
        echo "Problem uploading the file ... ";
        }
    }   
    else
    {
    echo "No file chosen !! ";
    }
    }
    else
    {
    echo "Button not clicked ";
    }
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM