dongming0505 2014-07-29 16:34
浏览 52

无法从源显示图像。 PHP / MySQL的

I Had written a previous post about this but since then I have made some changes and attempted other things. I am trying to allow users to upload a profile image to which can be displayed on their profile.

However I am able to get the upload script working, but when trying to display the image I seem to get a image box. I have come to find that the database table updates to the new image and saves it in a folder, then when trying to display that image I am either able to echo out the file name or just a blank image box.

Any help would be much appreciated (Y)

PHP to display image:

<?php echo '<img src="', $user_data['profile'], '" alt="', $user_data['name'], '\'s  Profile Image">'; ?>

PHP Image upload script (This works and I believe It may be something in the functions script):

        <?php
        if (isset($_FILES['profile']) === true) {
            if (empty($_FILES['profile']['name']) === true) {
                ?>
                <p>You have not chosen a image</p>

                <?php

            } else {
                $allowed = array('jpg', 'jpeg', 'gif', 'png');

                $file_name = $_FILES['profile']['name'];
                $file_extension = end((explode('.', $file_name)));
                $file_temp = $_FILES['profile']['tmp_name'];

                if (in_array($file_extension, $allowed) === true) {
                    change_profile_image($session_user_id, $file_temp, $file_extension);
                       header('Location: profile.php?success');
                    exit();

                } else {
                    echo 'Incorrect file type. Allowed: ';
                    echo implode(', ', $allowed);
                }
            }
        }

        if (empty($user_data['profile']) === false) {
            echo '<img class="profileimg" src="', $user_data['profile'], '" alt="', $user_data['name'], '\'s Profile Image">';
        }

        ?>
            <form action="" method="post" enctype="multipart/form-data">
              <input type="file" name="profile">
              <div class="submit3">
                <input type="submit" value="Upload" name="complete"/>
              </div>            
            </form>

Functions script:

function change_profile_image($user_id, $file_temp, $file_extension) {
  $file_path = 'images/profile/' . substr(md5(time()), 0, 10) . '.' . $file_extension;
  move_uploaded_file($file_temp, $file_path);
  mysql_query("UPDATE `users` SET `profile` = '" . mysql_real_escape_string($file_path) . "'  WHERE `user_id` = " . (int)$user_id);}

I believe it has something to do with the functions script but after days of changing the script I still cannot display the image after uploading. PS: Yes I know I should be using MSQLI functions, but I am just trying to finish off a college project to brush up on my PHP knowledge.

  • 写回答

2条回答 默认 最新

  • drb0901500211 2014-07-29 16:41
    关注

    We're probably missing quite a bit of info, but you're saving a relative path (images/profile/...) in the database, and using that as the src of your image. Is the right path as seen from a web client?

    To pinpoint the issue, check:

    • that the file is indeed saved where it should be
    • what the URL to access the image should be
    • if you can indeed load the image when accessing that URL directly
    • what URL appears in the <img src>
    • if they match :-)
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题