2017-03-01 60 views
0

我有一些上傳表單,file_exists在同一個請求的兩個地方爲同樣的條件返回不同的結果。以下是示例代碼。file_exists在兩個地方爲相同的條件返回不同的結果

$flag = file_exists($_FILES['image']['name']); // return TRUE 
move_uploaded_file(
    $_FILES['image']['tmp_name'], 
    'uploads/' . $_FILES['image']['name'] 
); 

require_once APPPATH . 'custom_classes/ImageResize.class.php'; 

$img_resize = new ImageResize($_FILES['image']['tmp_name']); // here is the Exception thrown 
$img_resize->set_resize_dimensions(650, 451); 
$img_resize->crop_image(); 
$img_resize->save_image('uploads/cropped.jpg'); 
$img_resize->free_resourses(); 

這裏是拋出異常的類構造函數。

public function __construct($filepath) 
{ 
    if (!file_exists($filepath)) // same condition as above, throws Exception 
    { 
     throw new Exception('File not found!'); 
    } 

    $this->get_source_dimensions($filepath); 
    $this->load_source_img($filepath); 
} 

這讓我瘋狂。我可以從文件系統傳遞臨時路徑,但我非常確定此代碼之前工作過,現在它給了我這個。難道我做錯了什麼?

+1

那麼,如果你移動它,它將不再存在於舊的位置...... – jeroen

回答

3

它說文件不存在的原因是因爲它不存在了。

您正在將文件從tmp_name位置移動到uploads文件夾中,然後在剛移出的tmp_name位置查找它。

0

一旦你移動了文件,那麼它不是在原來的位置了,這是一個「移動」而不是「複製」,因此:

$flag = file_exists($_FILES['image']['name']); // return TRUE 
$newlocation = 'uploads/'.$_FILES['image']['name']; 
move_uploaded_file(
    $_FILES['image']['tmp_name'], 
    $newlocation 
); 

require_once APPPATH . 'custom_classes/ImageResize.class.php'; 

$img_resize = new ImageResize($newlocation); // no more Exception thrown! 
$img_resize->set_resize_dimensions(650, 451); 
$img_resize->crop_image(); 
$img_resize->save_image('uploads/cropped.jpg'); 
$img_resize->free_resourses(); 

應該更好地爲您。 (不太清楚爲什麼你有tmp_name和name,但我假設你知道)

相關問題