0

这是我的表单,带有一个名为的文件输入字段photo

<form action = "spremi-film.php" method = "POST" enctype = "multipart/form-data">
    <div class="grid_2">Naslov:</div>
    <div class="grid_10"><input type="text" name="naslov" value="" /></div>

    <div class="grid_2">Žanr: </div>
    <div class="grid_10"><?php izborZanra();   ?></div>

    <div class="grid_2">Godina: </div>
    <div class="grid_10"><?php izborGodine();   ?></div>

    <div class="grid_2">Trajanje:</div>
    <div class="grid_10"><input type="text" name="trajanje" value="" /></div>

    <div class="grid_2">Izbor slike:</div>
    <!--<input type="hidden" name="MAX_FILE_SIZE" value="30000" />-->
    <div class="grid_10"><input type="file" name="photo"  /></div>


    <div class="grid_12"><input type="submit" value="SPREMI" /></div>
</form>

之后我把这段代码:

$uploaddir = '/slike';
$uploadfile = $uploaddir . basename($_FILES['photo']['name']);

//echo '<pre>';

if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile))
{
    echo 'Slika je uspješno spremljena!';
}
else
{
    echo 'Slika nije spremljena!';
}

//print_r($_FILES);

//echo '</pre>';

当我运行它时,我会undefined index: photo在浏览器中收到通知。

4

1 回答 1

6

问题是$_FILES['photo']仅在用户上传文件时定义,而不是在显示表单时第一次执行脚本期间定义。

在访问and行isset($_FILES['photo'])中的变量之前检查 with 。$uploadfile = ...if (move_uploaded_file(...

于 2012-11-10T23:36:07.803 回答