[英]Using recently uploaded image to update a database in PHP
I am trying to get the filename of an uploaded file so I can update a database, here is what I have at the moment. 我正在尝试获取上载文件的文件名,以便可以更新数据库,这是我目前所拥有的。 Using image Maniuplator
使用图像操纵器
https://gist.github.com/philBrown/880506 https://gist.github.com/philBrown/880506
<?php
require_once(dirname(__FILE__).'/../config.php');
require_once(dirname(__FILE__).'/ImageManipulator.php');
if ($_FILES['fileToUpload']['error'] > 0) {
echo "Error: " . $_FILES['fileToUpload']['error'] . "<br />";
} else {
// array of valid extensions
$validExtensions = array('.jpg', '.jpeg', '.gif', '.png', '.JPG', '.JPEG', '.PNG', '.GIF', '.bmp');
// get extension of the uploaded file
$fileExtension = strrchr($_FILES['fileToUpload']['name'], ".");
// check if file Extension is on the list of allowed ones
if (in_array($fileExtension, $validExtensions)) {
$newNamePrefix = time() . '_';
$manipulator = new ImageManipulator($_FILES['fileToUpload']['tmp_name']);
$width = $manipulator->getWidth();
$height = $manipulator->getHeight();
$centreX = round($width / 2);
$centreY = round($height / 2);
// our dimensions will be 80by80
$x1 = $centreX - 40; // 80
$y1 = $centreY - 40; // 80
$x2 = $centreX + 40; // 80
$y2 = $centreY + 40; // 80
// center cropping to 80by80
$newImage = $manipulator->crop($x1, $y1, $x2, $y2);
// saving file to uploads folder
$manipulator->save('../images/avatars/uploaded/' . $newNamePrefix . $_FILES['fileToUpload']['name']);
$user_image_1 = '$newNamePrefix';
$user_image_2 = $_FILES['fileToUpload']['name'];
$user_image_file = "{$domain}/images/avatars/uploaded/{$user_image_1}{$user_image_2}";
// Update the DB with new avatar
function set_new_avatar() {
global $con, $user_id, $user_image_file;
$sql = "UPDATE users SET user_image='$user_image_file' WHERE id='$user_id'";
mysqli_query($con, $sql);
}
set_new_avatar();
header('Location: ../index.php');
} else {
echo 'You must upload an image...';
}
}
?>
I have tried wrapping, $user_image_2 in (", also tried wrapping the variable in {} but nothing seems to work. 我曾尝试将$ user_image_2包装在(“中,也尝试将变量包装在{}中,但似乎无济于事。
The file is uploaded and the database is being updated with [domain/images/avatars/uploaded/], but the problem is as it's being renamed during the upload and I can't capture the name. 文件已上传,并且数据库正在使用[domain / images / avatars / uploaded /]更新,但是问题在于在上传过程中它被重命名了,我无法捕获名称。
I imagine it's a small syntax issue but I can't get my head around how to sort it out. 我想这是一个很小的语法问题,但我无法确定如何解决它。
Thank you in advanced 谢谢高级
There were 2 problems here, 1. User_image_1 was being passed as a variable and not a string. 这里有2个问题:1. User_image_1作为变量而不是字符串传递。
$user_image_1 = '$newNamePrefix';
needed to be 需要成为
$user_image_1 = $newNamePrefix;
The second was that user_id wasn't properly being passed to this screen and therefor it was blank. 第二个原因是user_id没有正确地传递到此屏幕,因此为空白。
I managed to find this out, by printing different aspects of the code rather than redirecting. 通过打印代码的不同方面而不是重定向,我设法找到了答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.