1

我正在使用 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??

如何解决这个问题?

4

2 回答 2

1

只需删除此行

$profile_image = $_POST['profilepic'];

在主代码中。

文件在 _FILES 数组中发送,因此未在 _POST 中设置,您会收到通知。

于 2013-02-02T06:14:57.093 回答
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;
}
于 2013-02-02T06:15:11.740 回答