donglaohua1671 2014-02-05 17:32
浏览 48
已采纳

为什么我的上传脚本不起作用? 没有错误

My upload form is

<form action="accept-file.php" method="post" enctype="multipart/form-data">
Your Photo: <input type="file" name="photo" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>

And accept-file.php is

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

//if they DID upload a file...
if($_FILES['photo']['name'])
{
    //if no errors...
    if(!$_FILES['photo']['error'])
    {
        //now is the time to modify the future file name and validate the file
        $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
        if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
        {
            $valid_file = false;
            $message = 'Oops!  Your file\'s size is to large.';
        }

        //if the file has passed the test
        if($valid_file)
        {
            //move it to where we want it to be
            move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
            $message = 'Congratulations!  Your file was accepted.';
        }
    }
    //if there is an error...
    else
    {
        //set that to be the returned message
        $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
    }
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']

?>

It is copied directly from a tutorial that apparently works ok. As you can see I've ever forced some error reporting at the top, but all I get when I submit the form is a blank screen (browser loads file-accept.php) and the uploaded file does not appear in uploads/ (chmoded to 777).

EDIT: I am now getting these errors:

Array ( [photo] => Array ( [name] => k3Jb9gv.jpg [type] => image/jpeg [tmp_name] => /tmp/phpzc4fLT [error] => 0 [size] => 384262 ) )

Notice: Undefined index: field_name in .............. on line 38, 39, 40, 41

  • 写回答

1条回答 默认 最新

  • dongtangjie0495 2014-02-05 17:36
    关注

    This line gives you the problems:

    $new_file_name = strtolower($_FILES['photo']['tmp_name']);
    

    You should change that to something else like:

    $new_file_name = strtolower($_FILES['photo']['name']);
    

    This is because otherwise you filename is the whole temporary url of your file (including directories). That will give you a warning that you are not allowed to upload it there.

    Also, you need to set $valid_file to true somewhere, probably before your check for the valid file.

    //if they DID upload a file...
    if($_FILES['photo']['name'])
    {
        print_r($_FILES);
        //if no errors...
        if(!$_FILES['photo']['error'])
        {
            $valid_file = true;
            //now is the time to modify the future file name and validate the file
            $new_file_name = strtolower($_FILES['photo']['name']); //rename file
            if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
            {
                $valid_file = false;
                $message = 'Oops!  Your file\'s size is to large.';
            }
    
            //if the file has passed the test
            if($valid_file)
            {
                //move it to where we want it to be
                move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
                $message = 'Congratulations!  Your file was accepted.';
            }
        }
        //if there is an error...
        else
        {
            //set that to be the returned message
            $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择