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

时间:2014-02-05 17:32:27

标签: php file-upload

我的上传表单是

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

accept-file.php

<?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']

?>

直接从教程中复制,显然可以正常工作。正如您所看到的,我曾在顶部强制进行过一些错误报告,但是当我提交表单时,我得到的只是一个空白屏幕(浏览器加载file-accept.php),上传的文件没有出现在{{1}中(chmoded to 777)。

修改 我现在收到这些错误:

  

数组([照片] =&gt;数组([名称] =&gt; k3Jb9gv.jpg [类型] =&gt;图片/ jpeg [tmp_name] =&gt; / tmp / phpzc4fLT [错误] =&gt; 0 [尺寸] =&gt; 384262))

     

注意:未定义的索引:第38,39,40,41行的..............中的field_name

1 个答案:

答案 0 :(得分:1)

这一行给出了问题:

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

您应该将其更改为其他内容:

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

这是因为否则你的文件名是文件的整个临时URL(包括目录)。这会给你一个警告,你不能在那里上传它。

此外,您需要在检查有效文件之前将$valid_file设置为true

//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'];
    }
}