php files上传错误,php-PHP上传文件的问题$_FILES['file']['error']

这段时间一直在啃《Head First PHP & MySQL》,这本书非常好,一步步教授怎么开发一个

PHP网站,一直照着一步步地敲代码,现在看到487页,在测试上传文件功能时,发现前面

的editprofile.php里面的 if ($_FILES['file']['error'] == 0) { 老是报错:

// Validate and move the uploaded picture file, if necessary

if (!empty($new_picture)) {

if ((($new_picture_type == 'image/gif') || ($new_picture_type == 'image/jpeg') || ($new_picture_type == 'image/pjpeg') ||

($new_picture_type == 'image/png')) && ($new_picture_size > 0) && ($new_picture_size <= MM_MAXFILESIZE) &&

($new_picture_width <= MM_MAXIMGWIDTH) && ($new_picture_height <= MM_MAXIMGHEIGHT)) {

if ($_FILES['file']['error'] == 0) {

// Move the file to the target upload folder

$target = MM_UPLOADPATH . basename($new_picture);

if (move_uploaded_file($_FILES['new_picture']['tmp_name'], $target)) {

// The new picture file move was successful, now make sure any old picture is deleted

if (!empty($old_picture) && ($old_picture != $new_picture)) {

@unlink(MM_UPLOADPATH . $old_picture);

}

}

else {

// The new picture file move failed, so delete the temporary file and set the error flag

@unlink($_FILES['new_picture']['tmp_name']);

$error = true;

echo '

Sorry, there was a problem uploading your picture.';

}

}

}

else {

// The new picture file is not valid, so delete the temporary file and set the error flag

@unlink($_FILES['new_picture']['tmp_name']);

$error = true;

echo '

Your picture must be a GIF, JPEG, or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) .

' KB and ' . MM_MAXIMGWIDTH . 'x' . MM_MAXIMGHEIGHT . ' pixels in size.';

}

}// Update the profile data in the databaseif (!$error) { if (!empty($first_name) && !empty($last_name) && !empty($gender) && !empty($birthdate) && !empty($city) && !empty($state)) { // Only set the picture column if there is a new picture if (!empty($new_picture)) { $query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " . " birthdate = '$birthdate', city = '$city', state = '$state', picture = '$new_picture' WHERE user_id = '" . $_SESSION['user_id'] . "'"; } else { $query = "UPDATE mismatch_user SET first_name = '$first_name', last_name = '$last_name', gender = '$gender', " . " birthdate = '$birthdate', city = '$city', state = '$state' WHERE user_id = '" . $_SESSION['user_id'] . "'"; } mysqli_query($dbc, $query); // Confirm success with the user echo '

Your profile has been successfully updated. Would you like to view your profile?'; mysqli_close($dbc); exit(); } else { echo '

You must enter all of the profile data (the picture is optional).'; }}

修改登录用户的信息并点击按钮时

1429592712_617758.jpg就会显示下图的错误信息,而且数据库里面的信息根本没有改变!!!!Notice: Undefined index: file in D:\phpStudy\WWW\Mismatch\editprofile.php on line 44第44行是: if ($_FILES['file']['error'] == 0) { 请问这是什么原因,怎么解决??代码完全是原书官网提供的,没有写错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
$_FILES是一个PHP超全局变量,用于在上传文件时获取上传的文件信息。$_FILES数组的用法是通过HTML表单中的<input type="file">将文件上传到web服务器,然后通过PHP脚本使用$_FILES数组来处理上传的文件。 关于多文件上传,可以通过在HTML表单中添加多个<input type="file">元素来实现。在PHP脚本中,$_FILES数组会自动处理多个上传文件。在处理多个上传文件时,$_FILES数组中的每个文件都有自己的键名。例如,如果有两个上传文件,其键名分别为file1和file2,则可以使用以下代码访问它们: ``` $file1 = $_FILES["file1"]; $file2 = $_FILES["file2"]; ``` 值得注意的是,$_FILES数组中的每个上传文件都有自己的属性。常见的属性包括:name、type、size、tmp_name和error。其中,name属性表示文件的原始名称,type属性表示文件的MIME类型,size属性表示文件的大小(以字节为单位),tmp_name属性表示文件在web服务器上的临时存储位置,error属性表示上传过程中是否出错。 在PHP脚本中,可以使用move_uploaded_file()函数将上传的文件从临时位置移动到指定的位置。例如,以下代码将上传的文件从临时位置移动到web服务器上的/uploads目录中: ``` $target_dir = "/uploads/"; $target_file = $target_dir . basename($_FILES["file"]["name"]); if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { echo "文件上传成功"; } else { echo "文件上传失败"; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值