上传文件后没有可视输出

时间:2014-04-15 17:04:00

标签: php file upload echo

所以我现在已经进行了一段时间的故障排除,而我仍然没有得到它......

我试图通过表单将文件上传到文件夹:

<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>

我通过&upload; file_php&#39;

评估此表单
//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";
?>

现在问题是它没有输出任何回声&#39;包含在第一个if语句和子语句中,而文件正确上传。

非常感谢任何帮助!

编辑:忘了&#39 ;;&#39;在最后一个回声结束时。

4 个答案:

答案 0 :(得分:1)

编辑:我今天从The Blue Dog (comment)了解了!!的内容,感谢您添加的PHP怪癖; - )

  

“信不信由你,!!就像没有第一名那样,尝试看看:$ t = true; if(!! $ t){echo”true“;}”/ /蓝狗

<击> 改变这一行:

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

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

它会起作用。 (测试)

两个!!都不应该存在。


<?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";
?>

答案 1 :(得分:0)

正确的属性是

enctype="multipart/form-data"

答案 2 :(得分:0)

我认为路径或许可存在问题。

$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;

答案 3 :(得分:0)

好的,有几件事要解决:

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";
        }
    }
}