[英]upload picture to and send path to database
I am trying to upload picture and link it to my database and I used the codes below : 我正在尝试上传图片并将其链接到我的数据库,并且我使用了以下代码:
Upload3.php Upload3.php
<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '1234', 'fyp');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);
// Create the SQL query
$query = "
INSERT INTO `pic` (
`Name`, `mime`, `size`, `data`, `created`
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', NOW()
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
// Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';
?>
and this is the form.php 这是form.php
<html>
<body>
<form action="upload3.php" method="post"
enctype="multipart/form-data">
<label for="uploaded_file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I reached the last stage and its give error a file was not sent 我到了最后一个阶段,它的给出错误文件未发送
I don't know where I have missed. 我不知道我在哪里错过。
thank you 谢谢
Change your form file name to "uploaded_file". 将您的表单文件名更改为“ uploaded_file”。 You're script is looking for a non existent file post by the name of "file" rather than "uploaded_file"
您正在脚本中查找名称为“ file”而不是“ uploaded_file”的不存在的文件
Check the file name: Upload2.php
or upload3.php
? 检查文件名:
Upload2.php
或upload3.php
? (careful case-insensitive). (注意区分大小写)。 Change the
name="file"
to name="uploaded_file"
. 将
name="file"
更改为name="uploaded_file"
。
form.php form.php
<html>
<body>
<form action="upload2.php" method="post" enctype="multipart/form-data">
<label for="uploaded_file">Filename:</label>
<input type="file" name="uploaded_file" id="uploaded_file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
In upload2.php
, you forgot the mime
field: 在
upload2.php
,您忘记了mime
字段:
$datetime = date("Y-m-d h:i:s");
// Create the SQL query
$query = "
INSERT INTO `pic` (
`Name`, `mime`, `size`, `data`, `created`
)
VALUES (
'{$name}', '{$mime}', {$size}, '{$data}', '{$datetime}'
)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.