是否可以同时存储和检索MySQL数据库中的数据

时间:2015-03-13 19:20:27

标签: php mysql mysqli

我想上传一个文件,我将存储名称,链接&文件的大小。然后,我想检索文件的链接,以便我可以阅读它&提取后将文件的内容存储在数据库中。

是否可以在单个PHP文件中执行此操作?现在,当我上传文件时,它会存储名称,链接&文件大小到数据库。但是为了阅读上传的文件,请提取内容和内容。将内容存储到数据库中,我必须重新上载同一个文件。

users.XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<user>
    <FirstName>Example 1</FirstName>
    <LastName>Example 2</LastName>
    <Phone>Example 3</Phone>
    <Address>Example 4</Address>
</user>

以下是PHP脚本:

<?php

//report any error
error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

//connect to database
include 'conn.php';

$ds = "/";
$storeFolder = 'uploads';


if(!empty($_FILES)) {
    $fname = basename($_FILES['file']['name']);
    $ftype = $_FILES['file']['type'];
    $fsize = $_FILES['file']['size'];
    $flink = 'http://localhost/aps/upload/' . rawurlencode($fname);

    $xml = simplexml_load_file($flink);

    $firstname = $xml->FirstName;
    $lastname = $xml->LastName;
    $phone = $xml->Phone;
    $address = $xml->Address;

    $tempFile = $_FILES['file']['tmp_name'];
    $targetPath = dirname(__FILE__) . $ds . $storeFolder . $ds;
    $targetFile = $targetPath . $_FILES['file']['name'];
    move_uploaded_file($tempFile, $targetFile);

    $query = "INSERT INTO user_details (link, name, size, type, first_name, last_name, phone, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

    $stmt = $conn->prepare($query);
    $stmt->bind_param('ssssssss', $flink, $fname, $fsize, $ftype, $firstname, $lastname, $phone, $address);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($link, $name, $size, $type, $first_name, $last_name, $phone, $address);


}

//Close statement
$stmt->close();

//Close connection
$conn->close();

?>

感谢帮助。感谢。

1 个答案:

答案 0 :(得分:0)

看起来你在上传文件后正在下载文件,尝试在执行move_uploaded_file后加载文件...并且你希望将$ targetFile的内容插入表user_details到字段“file_data”中尝试更改为

move_uploaded_file($tempFile, $targetFile);
$xml = simplexml_load_file( $targetFile);

$firstname = $xml->FirstName;
$lastname = $xml->LastName;
$phone = $xml->Phone;
$address = $xml->Address;

$query = "INSERT INTO user_details (link, name, size, type, first_name, last_name, phone, address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

$stmt = $conn->prepare($query);
$stmt->bind_param('ssssssss', $flink, $fname, $fsize, $ftype, $firstname, $lastname, $phone, $address);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($link, $name, $size, $type, $first_name, $last_name, $phone, $address);