【发布时间】:2014-05-26 13:27:43
【问题描述】:
因此,当用户上传照片时,我终于将图像存储在服务器上的文件夹中。我现在需要弄清楚的是,用户如何上传该照片(到文件系统文件夹),然后让该照片自动显示在 index.php 页面上,在内容包装器中。任何帮助都很棒。
我要显示的图像将在图像 div 内,它在照片 div 内,并且在 imageInfo div 的上方,其中包含图像的描述。
这是我的 index.php 页面的代码(它已经有一个示例图像供参考):
<div id="contentWrapper">
<?php echo "<div id='photo'><div id='image'><img src='https://i.imgur.com/1QJFnJz.jpg'><div id='imageInfo'></div></div></div>";?>
</div>
这是我的图片上传表单的代码:
<body>
<div id="contentWrapper">
<img style="margin-left:150px ; margin-top:70px" src="http://i.imgur.com/YjvuqaP.jpg" height="100">
<div id="postForm">
<form action="posts_check.php" method="POST" name="posts_form" id="posts_form" onSubmit="return validateFrom()" enctype="multipart/form-data">
<input type="file" name="image" id="image"><br>
<textarea name="description" id="description" placeholder="Enter a description of your work here...";></textarea><br>
<input type="submit" name="upload" id="upload" value="Upload">
</form>
</div>
<h2 style="font-family:Arial ; color:#9b6bb4">Before you upload your file:</h2>
<ul style="font-family:Arial">
<li>Your file must be smaller than 1MB, so if you need to compress your photo, visit <a style="text-decoration:none ; color:#9176FF" href="http://jpeg-optimizer.com/">JPEG-Optimizer</a></li><br>
<li>Make sure that your file is 'at least' <em><strong>480px</strong></em> wide. (If not, the image will look weird when uploaded)</li><br>
<li>Make sure that you are the owner of the work that you are uploading. (Copyright suits suck)</li><br>
<li>Make sure you include a detailed description of the work to give others an idea of how you did it.</li>
</ul>
</div>
这里是将上传的图片发送到服务器上的文件夹的代码:
<?php
session_start();
if(!isset($_SESSION['username'])) {
header('location: must_login.php');
}
$conn = mysqli_connect("localhost","root","") or die ("No SQLI");
mysqli_select_db($conn, "sample") or die ("No DB");
if (isset($_FILES['image']) && ($_FILES['image']['size'] < 2097152) && (in_array($_FILES['image']['type'], array('image/jpeg', 'image/png', 'image/jpg', 'image/gif')))) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$rand_dir_name = substr(str_shuffle($chars), 0, 15);
mkdir("photos/$rand_dir_name");
if (file_exists("photos/$rand_dir_name/".$_FILES["image"]["name"]))
{
echo $_FILES["image"]["name"]." Already exists";
}
else
{
move_uploaded_file($_FILES["image"]["tmp_name"],"photos/$rand_dir_name/".$_FILES["image"]["name"]);
//echo "Uploaded and stored in: phoyoss/$rand_dir_name/".@$_FILES["image"]["name"];
$username = 'ralston3';
$description = $_POST['description'];
$image_name = $_FILES['image']['name'];
$image_id_before_md5 = "$rand_dir_name/$image_name";
$image_id = md5($image_id_before_md5);
$date = date('Y-m-d');
$sqli = "INSERT INTO `photos` (username, description, image_name, image_id, post_date) VALUES ('ralston3','$description','$image_name','$image_id','$date')";
mysqli_query($conn, $sqli) or die ("No query");
header('location:index.php');
//$profile_pic_name = @$_FILES["profilepic"]["name"];
//$img_id_before_md5 = "$rand_dir_name/$profile_pic_name";
//$img_id = md5($img_id_before_md5);
//$profile_pic_query = mysql_query("INSERT INTO photos VALUES ('','test','$user','$date','$description','http://localhost/tutorials/findFriends/userdata/user_photos/$rand_dir_name/$profile_pic_name','no','$img_id')");
//header("Location: upload_photo.php");
}
} else {
header('location:error.php');
}
?>
【问题讨论】:
-
选择有问题的图像的行,使用循环获取和回显。然后是
<img src='".$row['image']."'>bay-sick-ah-lee。谷歌"fetch image from database"你最终会得到很多结果。 -
假设我想获取用户上传的最新图片所在的行。我会检索具有最高图像 ID 的行吗?假设我给每张图片一个唯一的 id auto_incremented?
-
假设您有一个日期/时间列,我相信您会这样做,您将按日期 ASC 排序/分组以显示最新图像,并为相关的特定用户使用 WHERE 子句。
标签: php filesystems