我正在使用php类上传 http://www.verot.net/php_class_upload.htm
我想尝试上传图片文件并调整其大小。 但是当我点击“保存”按钮时会出现问题。
注意:未定义的索引:第47行的C:\ xampp \ htdocs \ projects \ pakistanihaider \ admin \ profile.php中的profilepic
这是我到目前为止所做的事情
HTML 我的表格标签:
<form class="form-horizontal row-fluid" enctype="multipart/form-data" method="post" action="<?php $_PHP_SELF ?>">
输入文件标签:
<?php //Profile Picture ?>
<div class="form-row control-group row-fluid">
<label class="control-label span3" for="search-input">Profile Picture</label>
<div class="controls span7">
<div class="input-append row-fluid">
<input type="file" name="profilepic" class="spa1n6 fileinput" value="<?php echo $showprofilepic; ?>" id="search-input">
</div>
</div>
</div>
PHP 对于类上传,我包括类php文件。
error_reporting(E_ALL);
// we first include the upload class, as we will need it here to deal with the uploaded file
include('./include/imageupload/class.upload.php');
其次我使用了这个,因为我不知道他们为什么使用它,但是他们在演示页面中使用了这个php
//Picture/File Upload Function
// retrieve eventual CLI parameters
$cli = (isset($argc) && $argc > 1);
if ($cli) {
if (isset($argv[1])) $_GET['file'] = $argv[1];
if (isset($argv[2])) $_GET['dir'] = $argv[2];
if (isset($argv[3])) $_GET['pics'] = $argv[3];
}
// set variables
$dir_dest = (isset($_GET['dir']) ? $_GET['dir'] : '../userUploads/profile-pictures');
$dir_pics = (isset($_GET['pics']) ? $_GET['pics'] : $dir_dest);
现在点击表格按钮的主要代码..
if(isset($_POST['savebtn'])){
$full_name = $_POST['full_name'];
$job_title = $_POST['job_title'];
$email = $_POST['email'];
$dob = $_POST['dob'];
$mobile = $_POST['mobile'];
$phone = $_POST['phone'];
$nationality = $_POST['nationality'];
$profile_image = $_POST['profilepic'];
$address = mysql_real_escape_string($_POST['address']);
$about_me = mysql_real_escape_string($_POST['aboutme']);
$last_updated = date('F j, Y, g:i a');
// ---------- IMAGE UPLOAD ----------
// we create an instance of the class, giving as argument the PHP object
// corresponding to the file field from the form
// All the uploads are accessible from the PHP object $_FILES
$handle = new Upload($_FILES['profilepic']);
// then we check if the file has been uploaded properly
// in its *temporary* location in the server (often, it is /tmp)
if ($handle->uploaded) {
// yes, the file is on the server
// below are some example settings which can be used if the uploaded file is an image.
$handle->image_resize = true;
$handle->image_ratio_y = true;
$handle->image_x = 300;
// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
$handle->Process($dir_dest);
// we check if everything went OK
if ($handle->processed) {
// everything was fine !
$uploadresult = ' <b>File uploaded with success</b><br />';
$uploadresult .= ' <img src="'.$dir_pics.'/' . $handle->file_dst_name . '" />';
$info = getimagesize($handle->file_dst_pathname);
$uploadresult .= ' File: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>';
$uploadresult .= ' (' . $info['mime'] . ' - ' . $info[0] . ' x ' . $info[1] .' - ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB)';
} else {
// one error occured
$uploadresult = ' <b>File not uploaded to the wanted location</b><br />';
$uploadresult .= ' Error: ' . $handle->error . '';
}
// we now process the image a second time, with some other settings
$handle->image_resize = true;
$handle->image_ratio_y = true;
$handle->image_x = 300;
$handle->image_reflection_height = '25%';
$handle->image_contrast = 50;
$handle->Process($dir_dest);
// we check if everything went OK
if ($handle->processed) {
// everything was fine !
$uploadresult2 = ' <b>File uploaded with success</b><br />';
$uploadresult2 .= ' <img src="'.$dir_pics.'/' . $handle->file_dst_name . '" />';
$info = getimagesize($handle->file_dst_pathname);
$uploadresult2 .= ' File: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>';
$uploadresult2 .= ' (' . $info['mime'] . ' - ' . $info[0] . ' x ' . $info[1] .' - ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB)';
} else {
// one error occured
$uploadresult2 = ' <b>File not uploaded to the wanted location</b><br />';
$uploadresult2 .= ' Error: ' . $handle->error . '';
}
// we delete the temporary files
$handle-> Clean();
} else {
// if we're here, the upload file failed for some reasons
// i.e. the server didn't receive the file
$failedupload = ' <b>File not uploaded on the server</b><br />';
$failedupload .= ' Error: ' . $handle->error . '';
}
$success = update_profile($full_name, $dob, $nationality, $address, $mobile, $phone, $job_title, $about_me, $profile_image, $last_updated, $email);
}
我现在不使用image来更新数据库,这就是我在update_profile
函数中没有使用image变量的原因。
现在问题是每当我点击保存按钮时我得到undefined index
??
如何解决这个问题?
答案 0 :(得分:1)
只需删除此行
即可$profile_image = $_POST['profilepic'];
在主要代码中。
文件在_FILES数组中发送,因此未在_POST中设置,您会收到通知。
答案 1 :(得分:0)
您需要使用isset()
来确定表单元素是否已填写。
Undefined index
通知表示您正在访问不存在的数组项。例如:
$arr = array('a' => 'x', 'b' => 'y');
$c = $arr['c'];
// Notice: Undefined index...
为了避免通知,您需要先检查它是否存在:
if (isset($arr['c'])) {
$c = $arr['c'];
} else {
$c = FALSE;
}