【问题标题】:Ignore picture upload error忽略图片上传错误
【发布时间】:2012-08-30 19:40:17
【问题描述】:

我的编码有问题,上传工作正常,如果我没有上传任何图片,它会回显错误,如果我只上传一张图片,它不会显示任何错误消息,现在我想要如果没有要上传的内容,它不会回显错误消息,但我想保留错误消息,以防万一出现问题,我知道有问题。谢谢!

// 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>";
    }

【问题讨论】:

  • 有什么理由使用“.”.“/uploads/”而不是“../uploads/”
  • 感谢您通知我,一旦提供答案将删除它。
  • 那么什么会构成“错误”?

标签: php


【解决方案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.";
}

【讨论】:

  • 感谢所有帮助我的人,我已经想出这个解决方案将近 4 天了。
【解决方案2】:

你可以试试这个

$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

【讨论】:

  • 我想我自己解决了,虽然有点脑筋急转弯,但还是谢谢你的帮助。
猜你喜欢
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2016-01-02
  • 2018-06-22
相关资源
最近更新 更多