PHP MySql - move_uploaded_file()问题

时间:2014-05-17 12:55:53

标签: php

我是php的新手,我对此有疑问:

              public function storeUploadedImage( $image ) {


            if ( $image['error'] == UPLOAD_ERR_OK )
            {
              // Does the ShopItem object have an ID?
              if ( is_null( $this->id ) ) trigger_error( "ShopItem::storeUploadedImage(): Attempt to upload an image for an ShopItem 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->shopItemImg = 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( "ShopItem::storeUploadedImage(): Couldn't move uploaded file.", E_USER_ERROR );
                if ( !( chmod( $this->getImagePath(), 0666 ) ) ) trigger_error( "ShopItem::storeUploadedImage(): Couldn't set permissions on uploaded file.", E_USER_ERROR );
              }

              // Get the image size and type
              $attrs = getimagesize ( $this->getImagePath() );
              $imageWidth = $attrs[0];
              $imageHeight = $attrs[1];
              $imageType = $attrs[2];

              // Load the image into memory
              switch ( $imageType ) {
                case IMAGETYPE_GIF:
                  $imageResource = imagecreatefromgif ( $this->getImagePath() );
                  break;
                case IMAGETYPE_JPEG:
                  $imageResource = imagecreatefromjpeg ( $this->getImagePath() );
                  break;
                case IMAGETYPE_PNG:
                  $imageResource = imagecreatefrompng ( $this->getImagePath() );
                  break;
                default:
                  trigger_error ( "ShopItem::storeUploadedImage(): Unhandled or unknown image type ($imageType)", E_USER_ERROR );
              }

              // Copy and resize the image to create the thumbnail
              $thumbHeight = intval ( $imageHeight / $imageWidth *  SHOP_THUMB_WIDTH );
              $thumbResource = imagecreatetruecolor (  SHOP_THUMB_WIDTH, $thumbHeight );
              imagecopyresampled( $thumbResource, $imageResource, 0, 0, 0, 0,  SHOP_THUMB_WIDTH, $thumbHeight, $imageWidth, $imageHeight );

              // Save the thumbnail
              switch ( $imageType ) {
                case IMAGETYPE_GIF:
                  imagegif ( $thumbResource, $this->getImagePath(  SHOP_IMG_TYPE_THUMB ) );
                  break;
                case IMAGETYPE_JPEG:
                  imagejpeg ( $thumbResource, $this->getImagePath(  SHOP_IMG_TYPE_THUMB ),  SHOP_JPEG_QUALITY );
                  break;
                case IMAGETYPE_PNG:
                  imagepng ( $thumbResource, $this->getImagePath(  SHOP_IMG_TYPE_THUMB ) );
                  break;
                default:
                  trigger_error ( "ShopItem::storeUploadedImage(): Unhandled or unknown image type ($imageType)", E_USER_ERROR );
              }

              $this->update();
            }
          }

错误:

Warning: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Filename cannot be empty in C:\wamp\www\risa\classes\shopItem.php on line 69

var_dump($_FILES):
array (size=1)
  'image' => 
    array (size=5)
      'name' => string '55.jpg' (length=6)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\wamp\tmp\php9C26.tmp' (length=23)
      'error' => int 0
      'size' => int 175289

谢谢!

1 个答案:

答案 0 :(得分:1)

您使用move_uploaded_file( $tempFilename, $this->getImagePath(),  但是move_uploaded_file的第二个参数必须是一个文件名 move_uploaded_file( $tempFilename, $this->getImagePath().$tempFilename 要么 move_uploaded_file( $tempFilename, $this->getImagePath().$image['name']