简体   繁体   中英

unlink associated image from php

I am trying to unlink specified image which is linked with mysql table record ID.I can delete the record on mysql. I get Internal server error when i run this script,

Query:

I have two cameras in_cam and out_cam folders I am trying to swap lane the images from in_cam to out_cam my script copies the images from in_cam to out_cam but the image is not deleted in the in_cam.

 Put the entry into the other table
            $sql = "INSERT INTO $newCamTable (plate, nread, datetime, millisecs, nationality, image_name,image) SELECT plate, nread, datetime, millisecs, nationality, image_name,image  FROM $camTable $
            logit($sql);
            $result = $conn->Execute($sql);
            if (!$result)
              {
                print $conn->ErrorMsg();
              }

            $sql = "SELECT id FROM $newCamTable ORDER BY id DESC";
            $result = $conn->Execute($sql);
            if (!$result)
              {
                print $conn->ErrorMsg();
              }
            $row = $result->fields;
            $newId = $row['id'];

            $sql = "UPDATE $newCamTable SET name=\"$new_cam_name\", camera_id=\"$new_cam_id\" WHERE id=\"$newId\"";
            logit($sql);
            $result = $conn->Execute($sql);
            if (!$result)
              {
                print $conn->ErrorMsg();
              }
    This is what I AM TRYING TO DO.

            sql = """SELECT id FROM  $camTable where id = \"$tableEntryId\"  LIMIT 1";
            logit($sql);
            $result  = $conn->Execute($sql);
            $id =  etCommonGetImagePath($conn, $camName, $id) {
            $serverDir = $_SERVER['DOCUMENT_ROOT'];
            $imagesTop = "/cam_images";

            $idArray = str_split(strval($id));
            $idString = implode('/', $idArray);

            $webPath = $imagesTop . "/" . $camName . "/" . $idString . ".jpg";
            $full_path = $serverDir . $webPath;

            if (file_exists($full_path)) {
            unlink($idString);
            }*/


            // Delete the entry from the camera table
            $sql = "DELETE FROM $camTable WHERE id=\"$tableEntryId\" LIMIT 1";
            logit($sql);
            $result = $conn->Execute($sql);

您将必须使用服务器上的路径来删除图像,而不是URL。

unlink('/var/www/test/folder/images/image_name.jpeg');

The following should help

realpath — Returns canonicalized absolute pathname is_readable — Tells whether a file exists and is readable unlink — Deletes a file

Run your filepath through realpath, then check if the returned path exists and if so, unlink it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM