【发布时间】:2015-02-21 13:56:55
【问题描述】:
<?php
$image = $_FILES['image'];
//name of new file selected
$imagename = $image['name'];
//name of file in database
$filename = $row['filename'];
//folder to move image
$TARGET_PATH = "../$university/img/users/$category/$username/";
//if imagename in DB is different to image just selected
if ($filename <> $imagename) {
// *** Include the class
include("resize-class.php");
//if the filename in DB different from filename chosen
//delete old image
unlink("../$university/img/users/$oldcategory/$username/$filename");
//create random number and add that to the beginning of new imagename
$uniqueID = uniqid();
$imagetitle = $imagename;
$fileunique = $uniqueID;
$fileunique .= $imagetitle;
$filename = $fileunique;
//upload new selected file
$oldpath = $_FILES['image']['tmp_name'];
// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($oldpath, $TARGET_PATH)) {
// NOTE: This is where a lot of people make mistakes.
// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
$imagename = $fileunique;
$path = "../$university/img/users/$category/$usernameid/$imagename";
// *** 1) Initialise / load image
$resizeObj = new resize($path);
if (exif_imagetype($path) == IMAGETYPE_JPEG) {
$exif = exif_read_data($path);
$ort = $exif['IFD0']['Orientation'];
switch ($ort) {
case 1: // nothing
break;
case 2: // horizontal flip
$resizeObj->flipImage($public, 1);
break;
case 3: // 180 rotate left
$resizeObj->rotateImage($public, 180);
break;
case 4: // vertical flip
$resizeObj->flipImage($public, 2);
break;
case 5: // vertical flip + 90 rotate right
$resizeObj->flipImage($public, 2);
$resizeObj->rotateImage($public, -90);
break;
case 6: // 90 rotate right
$resizeObj->rotateImage($public, -90);
break;
case 7: // horizontal flip + 90 rotate right
$resizeObj->flipImage($public, 1);
$resizeObj->rotateImage($public, -90);
break;
case 8: // 90 rotate left
$resizeObj->rotateImage($public, 90);
break;
}
}
if (($resizeObj->width > 600) || ($resizeObj->height > 600)) {
// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
$resizeObj->resizeImage(400, 400, 'crop');
// *** 3) Save image
$resizeObj->saveImage($path, 95);
}
//change filename in database
$query = "UPDATE people SET filename=? WHERE id=? AND username=?";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $filename);
$stmt->bindParam(2, $id);
$stmt->bindParam(3, $username);
$stmt->execute();
}
}
?>
我正在尝试创建一个表单,用户可以在其中更改他们之前上传的图像。 我已经在上面发布了我的代码,它可以“取消链接”旧图像,但之后似乎没有做任何事情,没有错误。
我正在尝试获取代码:
- 根据数据库中存储的名称检查新选择的图像名称
- 如果它们不同,请删除文件夹中的旧图像
- 上传新图片,图片名称以随机数开头
- 使用新文件名更新数据库
任何想法我哪里出错了?感谢您的帮助
【问题讨论】:
-
检查
var_dump($_FILES)以查看您是否获得了所需的值。你也玩太多变量了,用$filename = uniqid().$imagename -
@Aditya 感谢您的回复! var_dump 返回array(0) { },如何正确获取上传文件的名称
-
由于
var_dump返回空数组,文件上传表单似乎有问题。请检查您是否在<form>标签中包含了enctype="multipart/form-data"。 -
@Aditya 感谢您的帮助,这是表单的问题,经过一些调整并添加了现在可以使用的 enctype!谢谢!!
标签: php image file-upload upload image-uploading