2014-01-09 41 views
0

我创建了一个上传卡的名称,类型,描述和图片。 以下是文件

<?php 
$labels=array("type"=>"type", 
       "CardName"=>"Card Name", 
       "Description"=>"Description", 
       "atk"=>"Attack", 
       "def"=>"Defend"); 

    echo "<form enctype='multipart/form-data' action='InsertCard.php' method='POST'>"; 
    echo "<h2>Insert new card </h2>"; 
    foreach($labels as $keys =>$values) 
    { 
     echo "$values <input type='text' name='$keys'/><br/>"; 
    } 
    //echo "<input type='hidden' name='MAX_FILE_SIZE' value='80000'/>"; 
    echo "Insert card <input type='file' name='pix' /><br/>"; 
    echo "<input type='submit' value='insert new cards'/>"; 
    echo "<input type='submit' name='return' value='return'/>"; 
    echo "</form>"; 

?> 

<?php 
$labels=array("type"=>"type", 
       "CardName"=>"Card Name", 
       "Description"=>"Description", 
       "atk"=>"Attack", 
       "def"=>"Defend"); 

if(@isset($_POST['return'])) 
{ 
    header("Location:ShowCatalog.php"); 
} 
include("connect.inc"); 
$connect=mysqli_connect($host,$username,$password,$dbname) or die("can't connect to server"); 

foreach($_POST as $keys =>$values) 
{ 
    if(empty($values)) 
    { 
     if($keys=='type' or $keys=='CardName' or $keys=='Description' or $keys=='picture') 
     { 
      $empty_values[]=$keys; 
     } 
    } 
    else 
    { 
     if($keys=='type') 
     { 
      if(!preg_match("/^[A-Za-z -]{4,15}$/",$values)) 
      { 
       $invalid_data[]=$keys; 
      } 
     } 
     elseif($keys=='CardName') 
     { 
      if(!preg_match("/^[A-Za-z '-]{4,30}$/",$values)) 
      { 
       $invalid_data[]=$keys; 
      } 
     } 
     elseif($keys=='Description') 
     { 
      if(!preg_match("/^[\"\:\(\);A-Za-z0-9., '-]{4,1000}$/",$values)) 
      { 
       $invalid_data[]=$keys; 
      } 
     } 
     elseif($keys=="atk" or $keys=="def") 
     { 
      if(!preg_match("/^[0-9]{3,5}$/",$values)) 
      { 
       $invalid_data[]=$keys; 
      } 
     } 
     elseif($keys=='picture') 
     { 
      if(!preg_match("/^[A-Za-z0-9., '-]{4,30}(.jpg)$/",$values)) 
      { 
       $invalid_data[]=$keys; 
      } 
     } 

    } 
} 
// i think i did something wrong here. 
foreach($_FILES['pix'] as $keys =>$values) 
{ 
     //if there is no file uploaded 
    if($keys=='tmp_name') 
    { 
     if($value="") 
     { 
      $invalid_pix[]="can not find picture<br/>"; 
     } 
    } 
    //if the file is not jpeg format 
    if($keys=='type') 
    { 
     if(!preg_match("/^image\/jpeg$/",$values)) 
     { 
      $invalid_pix[]="only jpeg files are allowed<br/>"; 
     } 
    } 
    // if the file size is over 80000 
    if($keys=='size') 
    { 
     if($values>=80000) 
     { 
      $invalid_pix[]="size is over than allowed"; 
     } 
    } 
} 



if(@sizeof($empty_values)>0 or @sizeof($invalid_data)>0 or @sizeof($invalid_pix)>0) 
{ 
    if(@sizeof($empty_values)>0) 
    { 
    $join=join(", ",$empty_values); 
    $msg="You forgot to input: $join<br/>"; 
    echo $msg; 
    } 
    if(@sizeof($invalid_data)>0) 
    { 
    $join=join(", ",$invalid_data); 
    $msg="Invalid data: $join"; 
    echo $msg; 
    } 
    if(@sizeof($invalid_pix)>0) 
    { 
     foreach($invalid_pix as $values) 
     { 
      echo $values."<br/>"; 
     } 
    } 
    echo "<form enctype='multipart/form-data' action='$_SERVER[PHP_SELF]' method='POST'>"; 
    echo "<h2>Insert new card </h2>"; 
    foreach($labels as $keys =>$values) 
    { 
     echo "$values <input type='text' name='$keys'/><br/>"; 
    } 
    //echo "<input type='hidden' name='MAX_FILE_SIZE' value='80000'/>"; 
    echo "Insert card <input type='file' name='pix' /><br/>"; 
    echo "<input type='submit' value='insert new cards'/>"; 
    echo "<input type='submit' name='return' value='return'/>"; 
    echo "</form>"; 
    exit(); 
} 
else 
{ 
    echo 'ok'; 
} 

不过,我收到我不能放出来的价值“找不到图片”的问题。我的意思是,当用户没有插入卡,并按提交我总是出现“只允许jpeg文件”,但现在“无法找到图片”。如何解决

回答

2

拼写错误 您在使用$value代替$values如果条件

检查出来,。

//if there is no file uploaded 
if($keys=='tmp_name') 
{ 
    if($value="") 
    { 
     $invalid_pix[]="can not find picture<br/>"; 
    } 
} 
+0

我修好了谢谢! –

+0

你永远是受欢迎的..:P –