2017-09-01 50 views
0

我写了这个函数,我用它来动态地上传图片到我的本地服务器。如何查看并返回if语句中使用的函数的结果?

我跑进了以下问题,是它里面产生的$image_name,所以当我打电话uploadImage function我不能返回$image_name,这样我可以将其插入到数据库中,我不知道如何返回变量。

public function uploadImage($data, $uploadLocation, $nameTag){ 
    if($data['size'] != 0) { 
     $errors  = array(); 
     $maxsize = 16777216; 
     $acceptable = array(
      'image/jpeg', 
      'image/jpg', 
     ); 
     $image_extension = pathinfo($data['name'], PATHINFO_EXTENSION); 

     //image_name variable I'm referring to 
     $image_name = uniqid($nameTag, true) . '.' . $image_extension; 

     if($data['size'] >= $maxsize) { 
      $errors[] = 'File too large. File must be less than 16 megabytes.'; 
     } else if($data['size'] == 0){ 
      $errors[] = 'You need to upload an image.'; 
     } 

     if((!in_array($data['type'], $acceptable)) || (empty($data['type']))) { 
      $errors[] = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; 
     } 

     if(count($errors) === 0) { 
      $moveFile = move_uploaded_file($data['tmp_name'], $uploadLocation . '/' . $image_name); 

      if($moveFile){ 
       return true; 
      } 
     } 
    } 

    return false; 
} 

这里我使用uploadImage函数。

$uploadImage = new UploadImages(); 

if($uploadImage->uploadImage($data['image_data'], 'uploads/img/instructions', 'instruction_')){ 

    //here I'd like to return the $image_name from the function 
    //I'm using PDO to insert the name in database 
    $sth = $db->prepare('UPDATE instructions SET image = :image WHERE id = :id'); 
    $sth->bindValue(':image', //name returned from the function, PDO::PARAM_STR); 
    $sth->bindValue(':id', $instructionsId, PDO::PARAM_INT); 
    $sth->execute(); 
} 

我计算过,在一段代码:

if($moveFile){ 
    return true; 
} 

我可能会返回$image_name,而不是真实的,但我不能确定如何我抓住它是可用的,因为它是内部的如果声明。

任何想法我们如何能够返回这些特定的数据,或者如何更好地重写代码以适应这些需求的建议将是非常好的。

+1

你做你说什么,回'$ image_name'或创建一个像'getImageName()'方法。 – Rasclatt

+0

@Rasclatt它并没有想到创建一个获取图像名称的方法。感谢您的指导。 – Craig

回答

1

而且我的评论,我也许会考虑一下类似这样的结构:

class UploadImages 
    { 
     # Save all your persisting variables 
     protected $errors = array(); 
     protected $image_name, 
        $success = false; 
     # You may want to make this editable in the future 
     protected $maxsize = 16777216; 
     # You may want to add more mimes later 
     protected $acceptable = array(
         'image/jpeg', 
         'image/jpg', 
       ); 
     # Make a listener 
     public function listen($data, $uploadLocation, $nameTag) 
     { 
      if(!empty($data['size'])) { 
       $image_extension = pathinfo($data['name'], PATHINFO_EXTENSION); 
       # Store the file name 
       $this->image_name = uniqid($nameTag, true) . '.' . $image_extension; 
       # Use the editable variable 
       if($data['size'] >= $this->maxsize) { 
        # Store error 
        $this->errors[] = 'File too large. File must be less than 16 megabytes.'; 
       } 
       # Check editable mime 
       if((!in_array($data['type'], $this->acceptable)) || (empty($data['type']))) { 
        $this->errors[] = 'Invalid file type. Only JPG, GIF and PNG types are accepted.'; 
       } 
       # Store the success 
       if(count($this->errors) === 0) { 
        $this->success = move_uploaded_file($data['tmp_name'], $uploadLocation . '/' . $this->image_name); 
       } 
      } else { 
       $this->errors[] = 'You need to upload an image.'; 
      } 
      # Return the object 
      return $this; 
     } 

     public function getFileName() 
     { 
      return $this->image_name; 
     } 

     public function isUploaded() 
     { 
      return $this->success; 
     } 

     public function getErrors() 
     { 
      return $this->errors; 
     } 

     public function hasErrors() 
     { 
      return (!empty($this->errors)); 
     } 
    } 
# Create the class, since the listen() method returns the object, you can 
# run that right off the top 
$uploadImage = (new UploadImages())->listen($data['image_data'], 'uploads/img/instructions', 'instruction_'); 
# Check if there are errors or if the upload itself failed 
if($uploadImage->hasErrors() || !$uploadImage->isUploaded()) { 
    # Write the error depending on which error occurred 
    echo ($uploadImage->hasErrors())? implode('<br />',$uploadImage->getErrors()) : 'Your upload failed do to an unknown error.'; 
} 
else { 
    # Fetch name on success 
    $img = $uploadImage->getName(); 
    $sth = $db->prepare('UPDATE instructions SET image = ? WHERE id = ?'); 
    $sth->execute(array($img,$instructionsId)); 
} 
1

您可以将它存储在UploadImages字段中,并编写一个获取它的方法。

0

您可以从函数返回$image_name。如果它将被执行为true条件,如果你的函数返回除false/null or 0以外的任何值。

if($moveFile){ 
    return $image_name; //you can add file name here 
} 

上传图片功能

//Following condition become true if function return file name and not `false` 
if($image_name = $uploadImage->uploadImage($data['image_data'], 'uploads/img/instructions', 'instruction_')){ 
    //You can use filename now 
    //here I'd like to return the $image_name from the function 
    //I'm using PDO to insert the name in database 
    $sth = $db->prepare('UPDATE instructions SET image = :image WHERE id = :id'); 
    $sth->bindValue(':image', //name returned from the function, PDO::PARAM_STR); 
    $sth->bindValue(':id', $instructionsId, PDO::PARAM_INT); 
    $sth->execute(); 
} 
0

有几种方法可以做到这一点。首先,您需要在成功而不是true时返回$ image_name。然后,你可以做

$filename = $uploadImage->uploadImage($data['image_data'], 'uploads/img/instructions', 'instruction_'); 
if($filename !== false){ //uploadImage returns false on error 
    ... 

if($filename = $uploadImage->uploadImage($data['image_data'], 'uploads/img/instructions', 'instruction_')){ 

在第二种方法中,单等操作设置$ filename来的函数调用的结果,整个语句也计算到的结果函数调用。我更喜欢第一种方法,因为它更易于阅读和理解。