[英]PHP move_uploaded_file not writing to directory with all writing permissions
我正在自学PHP,并且尝试在PHP中创建一个用于上传文件的函数。 但是,move_uploaded_file拒绝为我工作。 以下是我无法使用的功能。
此函数测试模仿类型是否正确,文件大小是否太大以及图像尺寸。 然后,它为文件创建一个随机名称,将其移动到正确的文件夹,然后将数据库中的路径更改为头像。
public function updateAvatar($avatar)
{
global $db; //PDO DB connection
$available_mimes = array(
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpg',
'iejpg' => 'image/pjpeg'); // Define an array with allowed filetypes
$image_info = getimagesize($avatar["tmp_name"]);
$image_width = $image_info[0];
$image_height = $image_info[1]; // Retrieve the image size
//Test if the uploaded avatar has the desired type, file size and dimensions.
if (in_array($avatar['type'], $available_mimes) && $avatar['size'] >= self::MAX_AVATAR_SIZE && $image_height <= 150 && $image_width <= 150)
{
$timeInSeconds = round(microtime(true) * 1000);
$filename = md5($_SESSION['username'] . $timeInSeconds) . ".jpg"; //Create a random filename with the username and time in milliseconds hashed.
if (move_uploaded_file($avatar['tmp_name'], "/Content/Avatar/" . $filename)) //Try to place the file in a folder
{
$statement = $db->prepare("UPDATE users SET avatar = :avatar WHERE id = :id");
$result = $statement->execute(array('avatar' => "Content/Avatar/" . $filename,
'id' => $_SESSION['id'])); //update filepath in DB
return $result;
} else
{
return false;
}
} else
{
return false;
}
}
函数move_uploaded_file始终返回false。 我已经在文件夹上设置了权限,以使用FileZilla为每个人写入权限。 它没有帮助。 我究竟做错了什么? 除了那可怕的代码。
函数move_uploaded_file使用的是来自根目录的路径,而不是来自Web服务器根目录的路径。 我忘了在文件路径中包含/ var / www /。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.