很抱歉,如果我放在这里的代码太长了。长句短。我想提交一个包含不同字段和图像字段的表单。当我在localhost上运行时,它工作正常,它验证图像文件和所有其他字段。
但是当我FTP到服务器时,如果我将一个随机文件放入图像字段,并将其余字段留空,那么我提交它,它不会进行验证并在几秒钟后打印出500错误代码。有谁知道发生了什么?
<!--//Checking field + submitting form-->
<?php
if(empty($_POST) === false)
{
$requiredField = array('title', 'price', 'description');
foreach($_POST as $key=>$value)
{
if(empty($value) && in_array($key, $requiredField) === true)
{
$error[] = $allFieldsAreRequired;
break 1;
}
}
//validation form
if(strlen($_POST['title']) > 70 || strlen($_POST['title']) < 6)
{
$error[] = $titleInvalid;
}
if(!validation_numberOnly($_POST['price']))
{
$error[] = $priceInvalid;
}
$file = $_FILES['file']['tmp_name'];
$image = '';
$image_type = '';
if(!empty($_FILES['file']['name']))
{
//addslashes prevent sql injection
$image = addslashes(file_get_contents($_FILES['file']['tmp_name']));
$image_name = addslashes($_FILES['file']['name']);
$image_size = getimagesize($_FILES['file']['tmp_name']);
$image_type = $image_size['mime'];
if($image_size == false)
{
$error[] = "File is not a image file";
}
if(filesize($file) > 400000)
{
$error[] = 'File is too big, we only accept 400kb';
}
}
//if success no errors, submit
if(empty($error) === true)
{
$product_data = array(
'title' => $_POST['title'],
'price' => $_POST['price'],
'category' => $_POST['categories'],
'description' => $_POST['description'],
'owner' => $user_data['username'],
'upload_date' => date("Y-m-d H:i"),
);
$product_image = array(
'imageData' => $image,
'imageType' => $image_type,
);
add_new_products($product_data, $product_image) ? header('Location: successful.php') : header($ErrorPageLocation);
exit();
}
}
?>
<div class="well">
<form class="form-horizontal form-max-size" action='' method="POST"
enctype="multipart/form-data">
<div class="form-group">
<label for="inputEmail3" class="col-sm-1 control-label">Title*</label>
<div class="col-sm-10">
<input type="text" id="title" class="form-control max-input-width-2" name="title" placeholder="e.g: Iphone5s white 16GB for sale..." value="<?php is_data_field_set('title'); ?>">
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-1 control-label">Price (£, GBP)*</label>
<div class="col-sm-10">
<input type="text" class="form-control max-input-width-2" id="price" name="price" placeholder=""
value="<?php is_data_field_set('price'); ?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">Categories*</label>
<div class="col-sm-10">
<select name="categories">
<?php
//print all categories
$all_categories = retrieve_product_categories();
for($i=0; $i<sizeof($all_categories); $i++)
{
echo '<option value='.$all_categories[$i]['category_id'].'>'. $all_categories[$i]['category_full_name'] . '</option>';
}
?>
</select> </div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-1 control-label">Image</label>
<div class="col-sm-10">
<input type="file" name="file" class="form-control max-input-width-2" id="file">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button class="btn btn-success" type="submit">Publish</button>
</div>
</div>
</form>
</div>