我的问题是当我提交按钮时页面中的内容消失了,然后当我刷新页面时图片正常显示所有内容。所以我不明白为什么提交后我必须刷新页面才能显示图片。
<div class="widget">
<h2>Hello, <?php echo $user_data['first_name'].' ' . $user_data['last_name']; ?>!</h2>
<div class="inner">
<div class="profile">
<?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_size = $_FILES['profile']['size'];
$file_extn = strtolower(end(explode('.', $file_name)));
$file_temp = $_FILES['profile']['tmp_name'];
if(in_array($file_extn, $allowed) == true && $file_size<(1024*1024) == true)
{
change_profile_image($session_user_id, $file_temp, $file_extn);
exit();
}
else if($file_size<(1024*1024) == false)
{
echo "Image file size max 1 MB";
}
else
{
echo 'Incorrect file type. Allowed: ';
echo implode(', ', $allowed);
}
}
}
if(file_exists($user_data['profile']))
{
echo '<img src="', $user_data['profile'], '" alt="', $user_data['first_name'], ' \'s Profile Image">';
}
else
{
echo '<img src="./images/profile/default.gif" height="180"><br>';
echo '<strong>Upload your picture</strong>';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="profile"> <input type="submit">
</form>
</div>
<ul>
<li>
<a href="logout.php">Log out</a>
</li>
<li>
<a href="<?php echo $user_data['username']; ?>">Profile</a>
</li>
<li>
<a href="changepassword.php">Change password</a>
</li>
<li>
<a href="settings.php">Settings</a>
</li>
</ul>
</div>
</div>
这是功能:
function change_profile_image($user_id, $file_temp, $file_extn)
{
$file_path = 'images/profile/' . substr(md5(time()), 0, 10) . '.' . $file_extn ;
move_uploaded_file($file_temp, $file_path);
mysql_query("UPDATE users SET profile = '" . $file_path . "' WHERE user_id = " . (int)$user_id);
}