2012-09-06 47 views
0

我有這個編碼的問題,上傳工作正常,如果我沒有上傳任何圖片它會回顯錯誤,如果我只上傳一張圖片,它不會顯示任何錯誤信息,現在我想讓它如果沒有上傳,它不會迴應錯誤消息,但我想保持錯誤信息,只是爲了防止出現問題我知道有問題。謝謝!忽略圖片上傳錯誤

// Upload begins!! 

if ($_FILES['ufile']['name'][0] != "") 
    { 
    $target_path = "."."/uploads/".$petname."/".$_FILES['ufile']['name'][0]; 

    copy($_FILES['ufile']['tmp_name'][0], $target_path); 
    $filesize1=$_FILES['ufile']['size'][0]; 
    } 


    if ($_FILES['ufile']['name'][1] != "") 
    { 
    $target_path1 = "."."/uploads/".$petname."/".$_FILES['ufile']['name'][1]; 

    copy($_FILES['ufile']['tmp_name'][1], $target_path1); 
    $filesize2=$_FILES['ufile']['size'][1]; 
    } 


    if ($_FILES['ufile']['name'][2] != "") 
    { 
    $target_path2 = "."."/uploads/".$petname."/".$_FILES['ufile']['name'][2]; 

    copy($_FILES['ufile']['tmp_name'][2], $target_path2); 
    $filesize3=$_FILES['ufile']['size'][2]; 
    } 

// Check for error!! 
    if($filesize1 || $filesize2 || $filesize3 != 0) 
    { 
    } 

// If got error, show message 
    else 
    { 
    echo "<div class='error'>ERROR : There seems to be problem uploading the pictures.</div>"; 
    } 
+2

沒有任何理由使用「/上傳/‘而不是「../uploads/」 –

+0

’。」。感謝您通知我,一旦提供答案,將刪除它。 – Furry

+1

那麼會構成一個「錯誤」呢? – Erik

回答

1

我設法解決它自己,底部這裏是我的解決方案: -

if ($_FILES['ufile']['name'][0] != "") 
{ 
$target_path = "."."/uploads/".$petname."/".$_FILES['ufile']['name'][0]; 

if (!copy($_FILES['ufile']['tmp_name'][0], $target_path)) 
{ 
    $a = 1; 
} 

} 

else 
{ 
    $a = 0; 
} 


if ($_FILES['ufile']['name'][1] != "") 
{ 
$target_path1 = "."."/uploads/".$petname."/".$_FILES['ufile']['name'][1]; 

if (!copy($_FILES['ufile']['tmp_name'][1], $target_path1)) 
{ 
    $b = 1; 
} 

} 

else 
{ 
    $b = 0; 
} 


if ($_FILES['ufile']['name'][2] != "") 
{ 
$target_path2 = "."."/uploads/".$petname."/".$_FILES['ufile']['name'][2]; 

if (!copy($_FILES['ufile']['tmp_name'][2], $target_path2)) 
{ 
    $c = 1; 
} 

} 

else 
{ 
    $c = 0; 
} 

if ($a || $b || $c == 1) 
{ 
    echo "Error! There is problem uploading the pictures."; 
} 
+0

感謝所有幫助過我的人,我一直在爲此解決方案差不多4天。 – Furry

1

你可以試試這個

$upload_errors = array( 
    "No errors.", 
    "Larger than upload_max_filesize.", 
    "Larger than form MAX_FILE_SIZE.", 
    "Partial upload.", 
    "No file.", 
    "Nothing because 5 doesn't exist", 
    "No temporary directory.", 
    "Can't write to disk.", 
    "File upload stopped by extension.", 
); 

if($_FILES['ufile']['error']==0) { // 0=No errors 
    // process 
} 
else 
{ 
    if($_FILES['ufile']['error']!=4) { // 4=Not uploaded 
     // Error occured other than error code 4(you don't want to show this) 
     echo $upload_errors[$_FILES['ufile']['error']].' !<br />'; 
    } 
} 

參考:PHP Manual

+0

我想我自己解決了這個問題,雖然腦子扭曲,不過謝謝你的幫助。 – Furry

+0

歡迎您:-) –