我是php编程的新手 我有允许用户在我的网站上上传图片的代码 这是html部分...
<div class="col-sm-5">
<input type="file" id="photo" name="photo">
</div>
这是php的一部分
if(isset($_POST['add'])){
$name = $_POST['name'];
$slug = slugify($name);
$category = $_POST['category'];
$price = $_POST['price'];
$description = $_POST['description'];
$filename = $_FILES['photo']['name'];
$conn = $pdo->open();
$stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM products WHERE slug=:slug");
$stmt->execute(['slug'=>$slug]);
$row = $stmt->fetch();
if($row['numrows'] > 0){
$_SESSION['error'] = 'Product already exist';
}
else{
if(!empty($filename)){
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$new_filename = $slug.'.'.$ext;
move_uploaded_file($_FILES['photo']['tmp_name'], '../images/'.$new_filename);
}
else{
$new_filename = '';
}
try{
$stmt = $conn->prepare("INSERT INTO products (category_id, name, description, slug, price, photo) VALUES (:category, :name, :description, :slug, :price, :photo)");
$stmt->execute(['category'=>$category, 'name'=>$name, 'description'=>$description, 'slug'=>$slug, 'price'=>$price, 'photo'=>$new_filename]);
$_SESSION['success'] = 'User added successfully';
}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
}
}
$pdo->close();
}
else{
$_SESSION['error'] = 'Fill up product form first';
}
header('location: products.php');
然后...显示图像
<img src="<?php echo (!empty($product['photo'])) ? 'images/'.$product['photo'] : 'images/noimage.jpg'; ?>" width="100%" class="zoom" data-magnify-src="images/large-<?php echo $product['photo']; ?>">
代码工作正常,除了允许用户仅上传一张图片...我该如何更改,以便用户可以上传四张图片.....请帮助我。谢谢
答案 0 :(得分:0)
HTML
<div class="col-sm-5">
<input type="file" id="photo" name="photo" multiple>
</div>
已编辑
$images = explode(',',$product['photo']));
foreach($images as $val){
<img src="<?php echo (!empty($val)) ? 'images/'.$val : 'images/noimage.jpg'; ?>" width="100%" class="zoom" data-magnify-src="images/large-<?php echo $val; ?>">
}
在数据库中,将text
的数据类型为photo
PHP
if(isset($_POST['add'])){
$name = $_POST['name'];
$slug = slugify($name);
$category = $_POST['category'];
$price = $_POST['price'];
$description = $_POST['description'];
$conn = $pdo->open();
$stmt = $conn->prepare("SELECT *, COUNT(*) AS numrows FROM products WHERE slug=:slug");
$stmt->execute(['slug'=>$slug]);
$row = $stmt->fetch();
if($row['numrows'] > 0){
$_SESSION['error'] = 'Product already exist';
}
else{
if(isset($_FILES['photo']['name'][0]) && $_FILES['photo']['size'][0] != 0 && $_FILES['photo']['error'][0] == 0)
{
$filesCount = count($_FILES['photo']['name']);
for($i = 0; $i < $filesCount; $i++) {
$ext = pathinfo($_FILES['photo']['name'][$i], PATHINFO_EXTENSION);
$new_filename = $slug.'_'date('Y-m-d h:i:s').'.'.$ext;
move_uploaded_file($_FILES['photo']['tmp_name'][$i], '../images/'.$new_filename);
$allfiles[] = $new_filename;
}
$uploaded_img = implode(',',$allfiles);
}
else{
$uploaded_img = '';
}
try{
$stmt = $conn->prepare("INSERT INTO products (category_id, name, description, slug, price, photo) VALUES (:category, :name, :description, :slug, :price, :photo)");
$stmt->execute(['category'=>$category, 'name'=>$name, 'description'=>$description, 'slug'=>$slug, 'price'=>$price, 'photo'=>$uploaded_img]);
$_SESSION['success'] = 'User added successfully';
}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
}
}
$pdo->close();
}
else{
$_SESSION['error'] = 'Fill up product form first';
}
header('location: products.php');