Pergunta

So i've been troubleshooting for a while now, and I still just don't get it...

I'm trying to upload a file to a folder via a form:

<form id="insert_movie" action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

I evaluate this form via 'upload_file.php':

//Upload the image
$allow = array("jpg", "jpeg", "gif", "png");

$todir = 'images/';

if(isset($_POST['submit'])){
    if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
    {
        $info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file

        if ( in_array( end($info), $allow) ) // is this file allowed
        {
            if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
            {
                echo "this doesn't work";
            }
        }
        else
        {
            // error this file ext is not allowed
            echo "this doesn't work either";
        }
    }
}
echo "however this works perfectly";
?>

Now the problem is that it doesn't output any 'echo' that is included in the first if-statement and children, while the file is uploaded correctly.

Any help is highly appreciated!

EDIT: forgot a ';' at the end of the last echo.

Foi útil?

Solução

Edit: I learned something today about !! from The Blue Dog (comment), thanks for the added PHP quirk ;-)

"Believe it or not, !! is the same as not having them there in the first place, try it and see: $t = true; if(!! $t){ echo "true";}" // The Blue Dog

Change this line:

if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?

to

if ( $_FILES['file']['tmp_name'] ) // is the file uploaded yet?

and it will work. (tested)

Both !! should not be there.


<?php

//Upload the image
$allow = array("jpg", "jpeg", "gif", "png");

$todir = 'images/';

if(isset($_POST['submit'])){
//    if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?

    if ( $_FILES['file']['tmp_name'] ) // is the file uploaded yet?
    {
        $info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file

        if ( in_array( end($info), $allow) ) // is this file allowed
        {
            if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
            {
                echo "Success";
            }
        }


        else
        {
            // error this file ext is not allowed
            echo "Sorry, this is now allowed.";
        }
    }

}
// echo "however this works perfectly";
?>

Outras dicas

The correct attribute is

enctype="multipart/form-data"

I think problem in path or permission.

$path = dirname(__FILE__).'/'.$todir;
if(is_dir($path)){
if ( move_uploaded_file( $_FILES['file']['tmp_name'], $path . basename($_FILES['file']['name'] ) ) )
            {
                echo "this doesn't work";
            }
}
else
echo 'Check path '.$path;

Okay, a couple of things to work out:

if(isset($_POST['submit'])){
    //not sure if what you wrote here was valid, but most commonly it's
    if (isset($_FILES['file']['name']) ) // does file exist
    {
        //we'll use an easier way to find the extension
        $filename = $_FILES['file']['name'];
        $ext = pathinfo($filename, PATHINFO_EXTENSION);

        if ( in_array( $ext, $allow) ) // is this file allowed
        {
            if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
            {
                echo "file uploaded";
            }
            else
            {
                echo 'error uploading: '.$_FILES['file']['error'];
            }

        }
        else
        {
            // error this file ext is not allowed
            echo "file not allowed";
        }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top