防止Class填充空值

时间:2011-12-12 19:56:51

标签: php upload

我在我的网站上写了一个上传脚本,用户可以上传预定义数量的项目(在我的情况下为12)。用户可以编辑他的项目(当然......) 所以我也写了一个更新脚本。从数据库中读取项目,用户可以重新上载或删除某些文件。但我所知道的是,当用户没有上传新文件时,原始文件会被空值覆盖(有意义,因为<input>为空。) 但我想阻止这一点。我该怎么做?

表格

$activities = Project::getProjectById($_DB, $projectnumber);
if(!empty($activities)) {
    foreach($activities as $k => $v) {
        echo "<input type='text' value='".$v['name']."' name = 'newproject_name' />";
        $i = 1;
        while ($i <= 12) {
        if(!empty($v["photo$i"])) {
           echo "<img src='../files/".$v["photo$i"]."' width='200' height='200'  />";
           echo "<input type='file' name='images[]' />";
        }   
        $i++;
        }
     }
     echo "<input type=submit value='Update' name='submitted'>";
     echo "</form>"; 
}

PHP

$project = new Project();
$project->name      = $_POST['newproject_name'];

$project->photo1    = $_FILES['images']['name'][0];
$project->photo2    = $_FILES['images']['name'][1];
$project->photo3    = $_FILES['images']['name'][2];
$project->photo4    = $_FILES['images']['name'][3];
$project->photo5    = $_FILES['images']['name'][4];
$project->photo6    = $_FILES['images']['name'][5];
$project->photo7    = $_FILES['images']['name'][6];
$project->photo8    = $_FILES['images']['name'][7];
$project->photo9    = $_FILES['images']['name'][8];
$project->photo10   = $_FILES['images']['name'][9];
$project->photo11   = $_FILES['images']['name'][10];
$project->photo12   = $_FILES['images']['name'][11];

if($project->updateProject($_DB, $projectnummer)) {
   $feedback = "<div class='feedback good'>ok!!</div>";
} else {
   $feedback = "<div class='feedback bad'>NOT ok!</div>";
}   

您不必担心getProjectByIdupdateProject - 函数,它们的工作正常。但正如您所看到的,脚本将上传所有空值并在数据库中覆盖它们。
我想阻止它!有什么建议?

修改

 if (!empty($_FILES['images']['name'][0])) {
        $project->photo1    = $_FILES['images']['name'][0];
  }
  if (!empty($_FILES['images']['name'][1])) {
    $project->photo2    = $_FILES['images']['name'][1];
  }
  if (!empty($_FILES['images']['name'][2])) {
    $project->photo3    = $_FILES['images']['name'][2];
  }
  if (!empty($_FILES['images']['name'][3])) {
    $project->photo4    = $_FILES['images']['name'][3];
  }
  if (!empty($_FILES['images']['name'][4])) {
    $project->photo5    = $_FILES['images']['name'][4];
  }
  if (!empty($_FILES['images']['name'][5])) {
    $project->photo6    = $_FILES['images']['name'][5];
  }
  if (!empty($_FILES['images']['name'][6])) {
    $project->photo7    = $_FILES['images']['name'][6];
  }
  if (!empty($_FILES['images']['name'][7])) {
    $project->photo8    = $_FILES['images']['name'][7];
  }
  if (!empty($_FILES['images']['name'][8])) {
    $project->photo9    = $_FILES['images']['name'][8];
  }
  if (!empty($_FILES['images']['name'][9])) {
    $project->photo10   = $_FILES['images']['name'][9];
  }
  if (!empty($_FILES['images']['name'][10])) {
    $project->photo11   = $_FILES['images']['name'][10];
  }
  if (!empty($_FILES['images']['name'][11])) {
    $project->photo12   = $_FILES['images']['name'][11];
  }

2 个答案:

答案 0 :(得分:1)

也许你应该使用is_uploaded_file()函数(LINK)?

修改

for($i = 0; $i <= 11; $i++) {
  if(!is_uploaded_file($_FILES['images']['name'][$i]))
     $project->photo[$i] = $_FILES['images']['name'][$i];
}

答案 1 :(得分:1)

$project->photo1    = $_FILES['images']['name'][0];
$project->photo2    = $_FILES['images']['name'][1];
$project->photo3    = $_FILES['images']['name'][2];
$project->photo4    = $_FILES['images']['name'][3];
$project->photo5    = $_FILES['images']['name'][4];
$project->photo6    = $_FILES['images']['name'][5];
$project->photo7    = $_FILES['images']['name'][6];
$project->photo8    = $_FILES['images']['name'][7];
$project->photo9    = $_FILES['images']['name'][8];
$project->photo10   = $_FILES['images']['name'][9];
$project->photo11   = $_FILES['images']['name'][10];
$project->photo12   = $_FILES['images']['name'][11];

可以改写为

for ($i = 0; $i <=11; $i++) {
    $photo = 'photo' . ($i + 1);
    $project->$photo   = $_FILES['images']['name'][$i];
}

您可以通过以下方式恢复所有编辑:

for ($i = 0; $i <=11; $i++) {
    $photo = 'photo' . ($i + 1);
    if ( ! empty($_FILES['images']['name'][$i])) {
        $project->$photo = $_FILES['images']['name'][$1];
    }
}