使用以下脚本我试图:
一切正常但图像名称未上传到数据库。我不明白错误,有人能帮助我吗? 谢谢。
<!--script view preview image-->
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#image')
.attr('src', e.target.result)
.width(200)
.height();
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
<!-- /script view preview image-->
<!--html form-->
<form action="nuovo-articolo.php?start=1" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-xs-4 col-md-4">
<label>Upload image</label>
<label>(Max 300Kb - jpg, jpeg, png)</label>
<input class="form-control" type="file" name="image" onchange="readURL(this);" />
</div><!-- /upload image -->
<div class="col-xs-4 col-md-4">
<label>Anteprima</label>
<p><img id="image" src="#" alt="Immagine" /></p>
</div><!-- /preview image -->
</div><!-- /row -->
<div class="row">
<div class="col-xs-1 col-md-1">
<input type="submit" class="btn btn-success" value="Inserisci" name="submit" />
</div><!-- /submit -->
</div><!-- /row -->
</form><!-- /form -->
<!--html form-->
<!-- php -->
<?php
//store img name file into database
if (!empty ($_GET["start"]) ) {
$sql = "INSERT INTO articoli
(image)
VALUES
(
'".$_POST["image"]."'
)";
//upload file into folder
if ($conn->query($sql) === TRUE) {
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
//file extension check
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="I formati accettati sono JPEG o PNG.";
}
//file dimension check
if($file_size > 301000){
$errors[]='La dimensione del file non deve superare 300Kb';
}
//move file into folder
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../../uploads/articoli/".$file_name);
echo "File caricato";
}else{
print_r($errors);
}
}
//redirect
header('Location: giacenze.php');
} else {
echo "Errore: " . $sql . "<br>" . $conn->error;
}
}
?>
<!-- /php -->
答案 0 :(得分:0)
将文件移动到文件夹时,请插入图像名称。在这里你得到$ filename。
//将文件移动到文件夹
如果(空($错误)== TRUE){
move_uploaded_file($file_tmp,"../../uploads/articoli/".$file_name);
echo "File caricato";
$sql = "INSERT INTO articoli (image) VALUES ('".file_name."' )";
}