我有一个表单的代码,用于将图片上传到我的网站并将信息保存到mysql数据库:
<form method='post'>
Album Name: <input type="text" name="title" />
<input type="submit" name="submit" value="create" />
</form>
<h4>Add Photo</h4>
<form enctype="multipart/form-data" method="post">
<?php
require_once 'config.php';
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(isset($_POST['upload'])){
$caption = $_POST['caption'];
$albumID = $_POST['album'];
$file = $_FILES ['file']['name'];
$file_type = $_FILES ['file']['type'];
$file_size = $_FILES ['file']['size'];
$file_tmp = $_FILES ['file']['tmp_name'];
$random_name = rand();
if(empty($file)){
echo "Please enter a file <br>";
} else{
move_uploaded_file($file_tmp, 'uploads/'.$random_name.'.jpg');
$ret = mysqli_prepare($mysqli, "INSERT INTO photos (caption, image_url, date_taken)
VALUES(?, ?, NOW())");
$filename = "uploads/" + $random_name + ".jpeg";
mysqli_stmt_bind_param($ret, "ss", $caption, $filename);
mysqli_stmt_execute($ret);
echo "Photo successfully uploaded!<br>";
}
}
?>
Caption: <br>
<input type="text" name="caption">
<br><br>
Select Album: <br>
<select name="album">
<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$result = $mysqli->query("SELECT * FROM albums");
while ($row = $result->fetch_assoc()) {
$albumID = $row['albumID'];
$title = $row['title'];
echo "<option value='$albumID'>$title</option>";
}
?>
</select>
<br><br>
Select Photo: <br>
<input type="file" name="file">
<br><br>
<input type="submit" name="upload" value="Upload">
</form>
这已成功将图片上传到我的上传内容中。文件夹以及我的mysql数据库,但是,我想放入图片网址&#34;上传/(随机名称生成).jpg&#34; 我没有用我当前的代码执行此操作,这些信息记录在&#39; image_url&#39;我照片表的列只是生成的随机数。没有&#34;上传/&#34;在开始和&#34; .jpg&#34;最后。
我应该提一下我照片表的架构是: caption,image_url,date_taken,imageID
任何帮助将非常感谢!! 提前谢谢你
答案 0 :(得分:1)
您正在使用+
(加号)符号连接,在此行中:
$filename = "uploads/" + $random_name + ".jpeg";
PHP使用点/句点来连接,而不是使用JS / C语言语法的加号:
$filename = "uploads/" . $random_name . ".jpeg";
错误检查会发出语法错误信号。