Reputation: 133
i created an upload card name, type ,description and picture. Here are the files
<?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';
}
However, I'm getting a problem that i can not out put the value "can not find picture". I mean when user doesn't insert the card and press submit i always showed up "only jpeg files are allowed" but now "can not find picture". How to fix that
Upvotes: 0
Views: 102
Reputation: 704
Spelling mistake
you used $value
instead of $values
in if condition
Check it out,.
//if there is no file uploaded
if($keys=='tmp_name')
{
if($value="")
{
$invalid_pix[]="can not find picture<br/>";
}
}
Upvotes: 2