duanqiu2064 2012-11-10 22:54
浏览 13
已采纳

PHP - 未定义的索引错误[关闭]

This is my form with a file input field named 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>

After that i put this code:

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

When I run this I get an undefined index: photo notice in my browser.

  • 写回答

1条回答 默认 最新

  • dongle2627 2012-11-10 23:36
    关注

    The problem is that $_FILES['photo'] is only defined when the user uploads a file, not during the first execution of the script while displaying the form.

    Check with isset($_FILES['photo']) before accessing the variable in the $uploadfile = ... and if (move_uploaded_file(... lines.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图