当用户上传图片时,有时图片会显示在个人资料文件夹中,有时则不会。每次文件路径都会正确更新,因为我可以在数据库中看到它发生变化,但图像没有进入配置文件夹。任何人都能发现我做错了吗?
member.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_extn = explode('.', $file_name);
$file_extn = strtolower(end($file_extn));//converts string to lowercase
$file_temp = $_FILES['profile']['tmp_name'];
if (in_array ($file_extn, $allowed) === true) {
//upload
change_image($MemberID,$file_temp,$file_extn);
}else {
echo 'Incorrect file type. Allowed: ';
echo implode(', ', $allowed);
}
}
}
user.php的
function change_image ($MemberID, $file_temp, $file_extn) {
$file_path = 'images/profile/' . substr(md5(time()), 0, 10) . '.' . $file_extn; //take current time, create md5 hash, 10 character figure
move_uploaded_file($file_temp, $file_path);
print_r ($file_path);
mysql_query("UPDATE `member` SET `profile` = '" . mysql_real_escape_string($file_path) . "' WHERE MemberID = " . $MemberID) ;//update database
}