我正在尝试将我的文件上传到服务器上的 profile/目录,我相信一切都可能正常工作......但是在上传时它认为我的 jpeg png 和 gifs 不是正确的文件类型。为什么要这样做。这里出了什么问题?我该如何解决?
function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = 'profile/' . substr (md5(time()), 0, 10) . '.' . $file_extn;
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);
}
if (isset($_FILES['profile']) === true) {
if (empty($_FILES['profile']['name']) === true) {
echo 'y u no choose file!';
} else {
$allowed = array ('jpg', 'jpeg', 'gif', 'png');
//this is the part i think may be brocken.
$file_name = $_FILES['profile']['name'];
$file_extn = strtolower(end(explode (' . ', $file_name)));
$file_temp = $_FILES['profile']['tmp_name'];
if (in_array($file_extn, $allowed) === true) {
change_profile_image($session_user_id, $file_temp, $file_extn);
header('Location: dontdelete.php');
exit();
}else {
echo 'y u no jpg or png or gif';
}
}
}
if (empty($user_data['profile']) === false) {
echo '<img src"', $user_data['profile'], '" alt="">';
}
最佳答案
更改 explode (' . ', $file_name)
至 explode ('.', $file_name)
您还需要检查其有效图像,而不仅仅是以图像扩展名结尾。
你还缺少=
在您的 img 输出中,您也可以连接而不是回显 3 次:
echo '<img src="'.$user_data['profile'].'" alt="">';
关于php - php 不接受所有图像扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13354400/