2012-09-06 62 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

欢迎您:-) –