图像上传 - 返回文件名而不是图像。

时间:2014-07-29 00:28:14

标签: php mysql image file-upload

我制作了一个脚本,允许用户上传个人资料图片。图像上传很好,我可以通过上传另一个来更新图像 - 但是当我已经上传时显示图像时出现问题,我得到文件名但不是图像。是因为我只是试图只是回应表名'profile'来保存图像。或者它是els的东西?

  • 另外如何修改脚本以仅允许上传500 x 500的图像?

图片上传脚本:

    <div class="image-upload">

        <?php
        if (isset($_FILES['profile']) === true) {
            if (empty($_FILES['profile']['name']) === true) {
                echo 'Please choose a file!';
            } 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);

                    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" class="" name="profile"> 
            <input type="submit" class"">
        </form>

    </div>

功能脚本:

 <?php
   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);


   }

  //Select database, database_name needs to be changed 
  mysql_select_db("lr"); 

 //get decoded image data from database 
 $result=mysql_query("SELECT * FROM images WHERE img='".$_GET['img']."'"); 

 //fetch data from database 
   $data=mysql_fetch_array($result); 
  $encoded=$data['data']; 

 //note: "$data['data']" is the row "data" in the table we made. 
 //The image ID would be "$data['img']" for example 


 //close connection 
 mysql_close($connection); 

 //decode and echo the image data 
  echo base64_decode($encoded); 

Profile.php(显示图片):

      <div class=".profileimage">
        <?php echo $user_data['profile']; ?>
      </div>

非常感谢任何评论或反馈,谢谢(Y)

0 个答案:

没有答案