无法从服务器的文件系统中调用图像

时间:2014-05-25 22:17:19

标签: php filesystems

因此,当用户上传照片时,我终于将我的图像存储在服务器上的文件夹中。我现在需要弄清楚的是,用户如何上传该照片(到文件系统文件夹),然后让该照片自动显示在内容包装器的index.php页面上。任何帮助都很棒。

我想要显示的图像将位于图片div内,图像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'); 

    }

?>

1 个答案:

答案 0 :(得分:0)

<?php
$sql=mysql_query("select id, username, description, image_name, image_id, post_date from photos ORDER BY id DESC");

echo "<div id='contentWrapper'>";
while($image=mysql_fetch_array($sql)){
echo "<div id='photo'><div id='image'><img src='https://yoursite.com/photos/$image[$image_id]/$image[$image_name]'><div id='imageInfo'></div></div></div>";
}
echo "</div>";
?>

注意你必须添加id标识符作为图像id(用INT创建新的id字段)image_id你提到abou正在处理文件夹名称,所以它必须包含随机值所以不能用于ORDER方法