上传图片时出现以下错误:
警告:move_uploaded_file(/images/profile767abbfeae82141.jpg) [function.move-uploaded-file]:无法打开流:/Applications/XAMPP/xamppfiles/htdocs/core/functions/users.php 中没有这样的文件或目录在线5
警告:move_uploaded_file() [function.move-uploaded-file]:无法将“/Applications/XAMPP/xamppfiles/temp/phpATXrDx”移动到/Applications/XAMPP/xamppfiles/htdocs/core 中的“/images/profile767abbfeae82141.jpg” /functions/users.php 在第 5 行
下面是调用函数的代码move_uploaded_file
:
function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = '/images/profile' . substr(md5(time()), 0, 15) .
'.' . $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);
}
这是可以上传图片的地方:
<?php
require 'core/init.php';
if (!(isset($_SESSION['user_id']))) {
header ("Location: index.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 = end($file_extn);
$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: index.php');
exit();
} else {
echo 'That file type is not allowed';
}
}
}
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
</head>
<div class="profile">
<?php
if (empty($user_data['profile']) === false) {
echo '<img src="', $user_data['profile'], '" alt="',
$user_data['first_name'],'\'s Profile Image">';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="profile"><br>
<input type="submit">
</form>
</div>
Hello <?php echo $user_data['first_name']; ?><br>
Your email is : <?php echo $user_data['email']; ?><br>
Your last name is: <?php echo $user_data['last_name']; ?><br>
And your username is: <?php echo $user_data['username']; ?><br>
We currently have <?php echo user_count(); ?> users.<br>
<a href="/<?php echo $user_data['username']; ?>"> Profile </a><br>
<a href="../changepassword.php"> Change Password </a><br>
<a href="../settings.php"> Settings </a><br>
<a href="logout.php"> Logout </a>
</html>