1

I'm exploring the file upload and text parsing capabilities of PHP, but my first step is buggy, and I can't figure it out. The goal of the code is to display a form to upload a text, and then display the value of the corresponding $_FILES array. However, it doesn't work - it runs without error but doesn't display print_r($_FILES['upload']). What am I missing?

This is my Index_txtParsing file:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>test</title>
    </head>
    <body>
        <form enctype="multipart/form-data" class="Txt_upload" method='POST'  action='index_txtParsing.php'>
            <label for="file">Enter upload here:</label>
            <input type='file' name='upload'/>
            <input type='submit' name='submit' value="upload here"/>
        </form>
    </body>
</html>

<?php
    if(isset($_POST['upload'])){
    $upload=$_POST['upload'];
    print_r($_FILES['upload']);
    }else{
    $upload="unknown";
    }
?>

EDIT: After incorporating the recommendations below, this code works:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>test</title>
    </head>
    <body>
        <form enctype="multipart/form-data" class="Txt_upload" method='POST'  action='index_txtParsing.php'>
            <label for="file">Enter upload here:</label>
            <input type='file' name='upload'/>
            <input type='submit' name='submit' value="upload here"/>
        </form>
    </body>
</html>

<?php
    if(isset($_FILES['upload'])){
    echo "Value of FILES['upload'] ";
    print_r($_FILES['upload']);
    echo "<br/>";
    }else{
    }
?>
1

7 Answers 7

3

To get the uploaded file use $_FILES instead of $_POST:

<?php
    if(isset($_FILES['upload'])){
    $upload=$_FILES['upload'];
    print_r($_FILES['upload']);
    }else{
    $upload="unknown";
    }
?>

Also please read the manual on File Uploads.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget set 777 permission for your upload/ directory. For ubuntu $sudo chmod -R 777 uploads/
2

Your if statement should look like that:

if ($_FILES['upload']['name']!="")

Comments

1

Try to clear the value of action="" and check for the submit element in _POST instead upload in _POST so you get:

<form enctype="multipart/form-data" method="POST" action="">
        <input type="file" name="upload" />
        <input type="submit" name="submit" value="upload here"/>
</form>

<?php if ( isset( $_POST['submit'] ) ) { print_r( $_FILES ); } ?>

This should work.

Comments

1
  1. First of all you are checking for $_POST['upload'] when you should be checking for $_POST['submit'] if you want to see if the form has been submitted, or fot $_FILES[upload']['name'] if you are checking for the filename and
  2. when you are trying to show $_POST['upload'] that is not available as it's a file so try something like this if you want the name of the file:

    if(isset($_POST['submit'])){
        print_r($_FILES['upload']);
        echo $_FILES['upload']['name'];
    }else{
        $upload="unknown";
    }
    

Update:

As i told you in my first point if you need to check if a file has been chosen for upload, try changing your if statement as such so you first check whether the form has been submitted, and afterwards if a file has been chosen:

if(isset($_POST['submit'])){
    if($_FILES['upload']['name']!="")
            $upload = $_FILES['upload']['name'];
    else
        $upload = "no file";
}else{
    $upload="form not submitted";
}

echo $upload;

This of course is just an example and not a complete solution. In real case scenarios you'd probably want to check file sizes, file type etc...

2 Comments

That works but it only tests whether the button submit has been clicked, not whether a file has indeed been uploaded.
@JDelage Check my update as i had already mentioned that in my first point
1

Don't look for the file in the POST array, look for it in the FILES array:

if(isset($_FILES['upload']['tmp_name']) && $_FILES['upload']['size']>0){ //make sure the file has been uploaded correctly
    $upload=$_FILES['upload'];
    print_r($_FILES['upload']);
}else{
    $upload="unknown";
}

$_FILES['name of field'] is an array that can be accessed as follows (from PHP.net):

$_FILES['userfile']['name']

The original name of the file on the client machine.

$_FILES['userfile']['type']

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

$_FILES['userfile']['size']

The size, in bytes, of the uploaded file.

$_FILES['userfile']['tmp_name']

The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES['userfile']['error']

Comments

0

you need

<input type="hidden" name="MAX_FILE_SIZE" value="same_or_lower_than_in_your_php.ini">

in your form

1 Comment

I don't think it's indispensable, but in any case I tried with it and still doesn't work.
0

I believe this is the direction you're looking to go:

// Check if you uploaded a file previously
if(isset($_POST['upload']))
{
    // Load the new file
    if (!empty($HTTP_POST_FILES['ufile']['tmp_name']))
    {
            // Extension? 
        $temp_name = basename($_FILES['ufile']['name']);
        $extension = explode(".", $temp_name);
        $extension = end($extension);
        $filename .= $extension;

        $path = $uploaddir . $filename;

        if ($ufile != none)
        {
            if (copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
            {
           // It copied to where ever you want it to go!    
            } 
            else
            //error 
        }
        else
        //error
    }
    else
    //error

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.