2

正如标题所暗示的那样,出现致命的 php 错误。完整的错误是

注意:未定义变量:第 95 行“位置”中的文章

致命错误:在第 95 行的“位置”中的非对象上调用成员函数 storeUploadedImage()

抛出错误的函数如下

public function storeUploadedImage( $image ) {

  if ( $image['error'] == UPLOAD_ERR_OK )
  {
    // Does the Article object have an ID?
    // Line 95 -----v
    if ( is_null( $this->id ) ) trigger_error( "Article::storeUploadedImage(): Attempt to upload an image for an Article object that does not have its ID property set.", E_USER_ERROR );

    // Delete any previous image(s) for this article
    $this->deleteImages();

    // Get and store the image filename extension
    $this->imageExtension = strtolower( strrchr( $image['name'], '.' ) );

    // Store the image

    $tempFilename = trim( $image['tmp_name'] ); 

    if ( is_uploaded_file ( $tempFilename ) ) {
      if ( !( move_uploaded_file( $tempFilename, $this->getImagePath() ) ) ) trigger_error( "Article::storeUploadedImage(): Couldn't move uploaded file.", E_USER_ERROR );
      if ( !( chmod( $this->getImagePath(), 0666 ) ) ) trigger_error( "Article::storeUploadedImage(): Couldn't set permissions on uploaded file.", E_USER_ERROR );
    }

尽管似乎与我的问题没有任何关系,但我已经通过 Google 寻找解决我的错误的方法。

帖子的其余部分是创建的,而不是图像。当我回来更新图像时,上传没有问题,没有错误,这令人费解。

id 的类构造部分是

public function __construct( $data=array() ) {
  if ( isset( $data['id'] ) ) $this->id = (int) $data['id'];

我也用它在类的开头定义 id

public $id = null;

我只能假设 Id 没有被正确传递$this->id并保持为空,但我不知道如何解决这个问题。尽管我对此也可能错了。

方法在这里被调用

if ( isset( $_POST['saveChanges'] ) ) {

// User has posted the articles edit form: save the new articles
$articles = new Article;
$articles->storeFormValues( $_POST );
$articles->insert();
if ( isset( $_FILES['image'] ) ) $article->storeUploadedImage( $_FILES['image'] );
header( "Location: admin.php?status=changesSaved" );

} elseif ( isset( $_POST['cancel'] ) ) {
4

1 回答 1

1

您的对象称为$articlesnot $article,更改为:

if ( isset( $_FILES['image'] ) ) $articles->storeUploadedImage( $_FILES['image'] );
                                     //  ^ note: s
于 2013-01-04T15:11:49.437 回答