简体   繁体   English

将文件上传到文件夹不起作用

[英]uploading files to a folder is not Working

I have a problem with my code. 我的代码有问题。 The function MOVE_UPLOADED_FILE is not adding the file to the destiny folder. MOVE_UPLOADED_FILE函数未将文件添加到目的地文件夹。 It inserts the name to the database correctly but the folder still empty. 它将名称正确插入数据库中,但文件夹仍然为空。 I'm not experienced on uploading files so... I hope you can help me. 我没有上传文件的经验,所以...希望您能为我提供帮助。

<?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");

?>

Make sure you added the enctype attribute to your HTML Form tag: 确保已将enctype属性添加到HTML表单标签中:

<form method="POST" action="#" enctype="multipart/form-data">

Server side handling file upload: 服务器端处理文件上传:

<?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';
}
?>

Optional: if you are on a Unix or Linux-based OS, the 'images' directory must be writable (chmod 777 ./images) 可选:如果您使用的是基于Unix或Linux的操作系统,则“ images”目录必须可写(chmod 777 ./images)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM