繁体   English   中英

PHP MySql-move_uploaded_file()问题

[英]PHP MySql - move_uploaded_file() issue

我是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

谢谢!

您使用move_uploaded_file( $tempFilename, $this->getImagePath() ,但move_uploaded_file的第二个参数必须是例如move_uploaded_file( $tempFilename, $this->getImagePath().$tempFilenamemove_uploaded_file( $tempFilename, $this->getImagePath().$image['name']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM