删除上传的图片,上传新图片并更改数据库参考

时间:2014-12-23 12:10:55

标签: php image file-upload upload image-uploading

<?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();
    }
}
?>

我正在尝试创建一个表单,用户可以在其中更改之前上传的图像。 我已经在上面发布了我的代码,它取消了&#39;取消链接&#39;旧图像,但似乎没有做任何事情,没有错误。

我正在尝试将代码添加到:

  • 根据存储在数据库中的名称检查新选择的图像名称
  • 如果不同则删除文件夹中的旧图像
  • 使用图片名称开头的随机数上传新图片
  • 使用新文件名
  • 更新数据库

我出错的任何想法?谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

以下是对代码的一些想法:

  1. 根据Aditya的建议使用sprintf('%s%s', uniqid(), $imagename)
  2. 之类的内容
  3. 检查$ _FILES是否有任何错误(可能文件太大)
  4. 仅在宽度或高度超过600像素时保存图像(不确定旋转是否会自动保存图像)
  5. 检查您是否有足够的权限写入目录(可能需要先创建文件夹)