用php上传文件并保存到sql的路径 [英] upload file with php and save path to sql

查看:59
本文介绍了用php上传文件并保存到sql的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何使用php上传文件并将文件路径保存到sql服务器的任何很好的教程吗?

Does anyone know any good tutorial on how to upload a file with php and save the files path to a sql server?

推荐答案

要上传文件,您至少需要具有字段以浏览文件,并使用提交按钮提交表单.

To upload a file you need at least a HTML POST form with multipart/form-data encoding. Therein you put an input type="file" field to browse the file and a submit button to submit the form.

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

upload.php中, $_FILES 以字段名称为键.

In the upload.php the uploaded file is accesible by $_FILES with the field name as key.

$file = $_FILES['file'];

您可以按以下方式获取其名称:

You can get its name as follows:

$name = $file['name'];

您需要使用 move_uploaded_file() ,否则它将丢失:

You need to move it to a permanent location using move_uploaded_file(), else it will get lost:

$path = "/uploads/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
    // Move succeed.
} else {
    // Move failed. Possible duplicate?
}

您可以按照通常的方式将路径存储在数据库中:

You can store the path in database the usual way:

$sql = "INSERT INTO file (path) VALUES ('" . mysqli_real_escape_string($path) . "')";
// ...

这篇关于用php上传文件并保存到sql的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆