2014-04-19 36 views
0

下面是在執行時拋出一些錯誤的代碼。我想要做的是無論發生什麼(錯誤或沒有錯誤),代碼的最後一行都會被執行。獲取執行的特定代碼行「No Matter What」

<?php 
require 'main.php'; 
function create_photo($file_path) { 
    # Upload the received image file to Cloudinary 
    @$result = \Cloudinary\Uploader::upload($file_path, array(
    "tags" => "backend_photo_album", 
)); 
    @unlink($file_path); 
    error_log("Upload result: " . \PhotoAlbum\ret_var_dump($result)); 
    $photo = \PhotoAlbum\create_photo_model($result); 
    return $result; 
} 
$files = $_FILES["files"]; 
$files = is_array($files) ? $files : array($files); 
$files_data = array(); 
foreach ($files["tmp_name"] as $index => $value) { 
    array_push($files_data, create_photo($value)); 
} 
?> 
<script>window.location.replace('index.html')</script> 

任何幫助將不勝感激。由於

+0

你應該閱讀錯誤處理.. [_try..catch..finally .._](https://athos.blogs.balabit.com/2011/02/try-catch-finally-in-php/ ) –

+0

@PeeHaa,STFU運營商? Oo –

+0

@ShankarDamodaran http://www.php.net/manual/en/language.operators.errorcontrol.php – PeeHaa

回答

1

我認爲這取決於你的PHP版本,你可以使用一個「的try/catch /終於」集團這樣的:

try 
{ 
    // code that may throw an exception 
} 
catch(Exeption $e) // The exception you want to catch 
{ 
    // Exception treatment 
} 
finally 
{ 
    // Executed no matter what 
} 

也許就來看看如何使用它。