2017-09-01 52 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來的函數調用的結果,整個語句也計算到的結果函數調用。我更喜歡第一種方法,因爲它更易於閱讀和理解。