我的代码有问题。 MOVE_UPLOADED_FILE函数未将文件添加到目的地文件夹。它将名称正确插入数据库中,但文件夹仍然为空。我没有上传文件的经验,所以...希望您能为我提供帮助。
<?php
// configuration
include('../connect.php');
// new data
$id = $_POST['memi'];
$a = $_POST['code'];
$z = $_POST['gen'];
$b = $_POST['name'];
$c = $_POST['exdate'];
$d = $_POST['price'];
$e = $_POST['kategori'];
$f = $_POST['qty'];
$g = $_POST['o_price'];
$h = $_POST['profit'];
$i = $_POST['date_arrival'];
$j = $_POST['sold'];
$nama_file = $_POST['namaFile'];
if (! empty($_FILES['namaFile']['tmp_name'])) {
$nama_file = $_FILES['namaFile']['name'];
$nama_file = stripslashes($nama_file);
$nama_file = str_replace("'","",$nama_file);
$nama_file = str_replace(" ","-",$nama_file);
$nama_file = $a.".".$nama_file;
move_uploaded_file($_FILES['namaFile']['tmp_name'], "images/".$nama_file);
}
else {
echo "error";
}
// query
$sql = "UPDATE products
SET product_code=?, gen_name=?, product_name=?, expiry_date=?, price=?,kd_kategori=?, qty=?, o_price=?, profit=?, date_arrival=?, qty_sold=?,file_gambar=?
WHERE product_id=?";
$q = $db->prepare($sql);
$q->execute(array($a,$z,$b,$c,$d,$e,$f,$g,$h,$i,$j,$nama_file,$id));
header("location: products.php");
?>
答案 0 :(得分:0)
确保将enctype属性添加到HTML表单标签:
<form method="POST" action="#" enctype="multipart/form-data">
服务器端处理文件上传:
<?php
//some code here
if(isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
$tmpfname = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
//make some security checks here
if(!@move_uploaded_file($tmpfname, 'images/'.$filename)){
echo 'error';
} //else file uploaded successfuly
} else {
echo 'Upload failed or no file selected';
}
?>
可选:如果您使用的是基于Unix或Linux的操作系统,则“ images”目录必须可写(chmod 777 ./images)