文件上传表单问题

时间:2015-09-16 13:54:00

标签: php html

因此,我正在构建此控制面板,并且在其中一个页面上我已经包含了文件上传输入,因此用户可以上传图像。

问题是,当我试图上传名为" jappinner.jpg"例如,它工作得很好,但是当我试图上传名为" ELECTRIC_2WAY_PLASTIC_WITH-Z.jpg"表单只是崩溃,没有$ _POST变量发布到操作页面。

我现在已经坚持这个问题一段时间了,我真的无法理解......

表示代码:



<form method="post" enctype="multipart/form-data" name="submit" action="manage.php?page=products">
		<input type="hidden" name="new_product">
  <div class="form-group">
    <label for="exampleInputEmail1">Product Name</label>
    <input type="text" name="product_name" class="form-control" id="exampleInputEmail1" placeholder="Enter product name..">
  </div>

  <div class="form-group">
    <label for="example-getting-started">Image</label><br />
        <!-- The file input field used as target for the file upload widget -->
		<input type="file" name="photo" id="photo" /> 
	</div>

	
	<div class="form-group">
    <label for="example-getting-started">Catalog</label><br />
        <!-- The file input field used as target for the file upload widget -->
		<input type="file" name="catalog" id="catalog" /> 
	</div>
	
	<div class="form-group">
    <label for="example-getting-started">Product Drawing</label><br />
        <!-- The file input field used as target for the file upload widget -->
		<input type="file" name="product_drawing" id="product_drawing" /> 
	</div>
	
	<div class="form-group">
    <label for="example-getting-started">Technical Information</label><br />
        <!-- The file input field used as target for the file upload widget -->
		<input type="file" name="technical_information" id="technical_information" /> 
	</div>
	
	
	
  <div class="form-group">
    <label for="exampleInputEmail1">Product Category</label>
  <select id="category" name="category" class="form-control">
  <option value="agriculture">Agriculture</option>
  <option value="waterworks">Waterworks</option>
  <option value="fire_protection">Fire Protection</option>
  <option value="accessories_pilots">Accessories / Pilots</option>
</select>
 </div>
 
 <div class="form-group" id="agriculture_sub_categories">
    <label for="exampleInputEmail1">Sub Category</label>
  <select name="sub_category" class="form-control">
  <option value="agriculture">Metal Valves</option>
  <option value="waterworks">Plastic Valves</option>
</select>
 </div>
 
  <div class="form-group" id="accessories_pilots_sub_categories">
    <label for="exampleInputEmail1">Sub Category</label>
  <select name="sub_category" class="form-control">
  <option value="agriculture">Metal</option>
  <option value="waterworks">Plastic</option>
</select>
 </div>
 
  <div class="form-group">
    <label for="example-getting-started">Product Description</label>
	<textarea name="description" class="form-control" rows="10" placeholder="Enter product description here.."></textarea>
 </div>
 
 
  <button type="submit" name="submit" class="btn btn-primary">Add Product</button>
  <a href="manage.php?page=products" class="btn btn-default">Cancel</a>
</form>
&#13;
&#13;
&#13;

继承PHP代码:

 if(isset($_POST['new_product'])) {

 function replaceAll($string, $array = array(), $rpl) {
 foreach($array as $value) {
 $string = str_replace($value, $rpl, $string);
 }
 return $string;
 }


$slogen = replaceAll($_POST['product_name'], array('#', ' ', "'", ':', '"', '_', '&', '/', '?', ')', '('), '_');
$slogen = replaceAll($slogen, array('--'), '_');

$columns = array('name', 'slogen', 'category', 'sub_category', 'description');
$values = array($_POST['product_name'], $slogen, $_POST['category'], '', $_POST['description']);
$db->Add('products', $columns, $values);
$new_product_id = $db->ID;


include "../core/plugins/flourish/init.php";

// Product Picture
if(isset($_POST['photo'])) {
$uploader = new fUpload();
$uploader->setMIMETypes(array('image/gif','image/jpeg','image/jpg','image/pjpeg','image/png'),'The file uploaded is not an image');
$file = $uploader->move('../dist/images/products/', 'photo');
$columns = array("file", "role", "product_id");
$values = array("dist/images/products/".$uploader->final_name, "image", $new_product_id);
$db->Add("product_files", $columns, $values);
}

// Product Catalog
if(isset($_POST['catalog'])) {
$uploader = new fUpload();
$uploader->setMIMETypes(array('application/pdf'),'The file uploaded is not an PDF');
$file = $uploader->move('../dist/files/', 'catalog');
$columns = array("file", "role", "product_id");
$values = array("dist/files/".$uploader->final_name, "catalog", $new_product_id);
$db->Add("product_files", $columns, $values);
}

// Product Product Drawing
if(isset($_POST['product_drawing'])) {
$uploader = new fUpload();
$uploader->setMIMETypes(array('application/pdf'),'The file uploaded is not an PDF');
$file = $uploader->move('../dist/files/', 'product_drawing');
$columns = array("file", "role", "product_id");
$values = array("dist/files/".$uploader->final_name, "product_drawing", $new_product_id);
$db->Add("product_files", $columns, $values);
}

// Product Technical Information
if(isset($_POST['technical_information'])) {
$uploader = new fUpload();
$uploader->setMIMETypes(array('application/pdf'),'The file uploaded is not an PDF');
$file = $uploader->move('../dist/files/', 'technical_information');
$columns = array("file", "role", "product_id");
$values = array("dist/files/".$uploader->final_name, "technical_information", $new_product_id);
$db->Add("product_files", $columns, $values);
}


//redirect("manage.php?page=products&msg=new_product_success");

}

1 个答案:

答案 0 :(得分:1)

我们在这里处理$_FILES而不是$_POST

您需要更改

if(isset($_POST['photo']))

if(isset($_FILES['photo']))

并为所有其他人做同样的事。

参考:

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:只应在暂存时进行显示错误,而不是生产。

  • 如果崩溃,请检查您的日志。

还要确保文件夹具有适当的权限才能写入。

另外,您的表单标记包含name="submit"和提交按钮一样;这将是一场冲突。

另外,表单不包含name属性,应该从中删除。