Search code examples
phpmove

Move File in parent directory


I think the image says everything, but will explain my problem, I uploaded some images over php on the server and they are uploaded on the PHP directory (I don't know why), now I want to move them in the "sister" directory Bilder and their "child" Beschreibung.enter image description here

     $query ="SELECT id FROM hausaufgaben ORDER BY id ASC";
$res = mysqli_query($connect,$query);
$id = 0;

while($row = mysqli_fetch_array($res)){
$id = $row['id'];
}

$path = "Beschreibung_$id.png";

$actualpath = "http://mywebsite.com/Hausaufgabenplaner/Bilder/Beschreibung/$path";


$mydate=getdate(date("U"));
$Erstellungsdatum = "$mydate[weekday], $mydate[mday] $mydate[month], $mydate[year]";


$query = " Insert into hausaufgaben(beschreibung,Abgabedatum,Erstellungsdatum,Fach,DeviceID, Beschreibung_Image) values ('$beschreibung','$Abgabedatum', '$Erstellungsdatum','$Fach','$DeviceID','$image');";
         if(mysqli_query($connect,$query)){
        file_put_contents($path,base64_decode($image));
        rename("$path", "/Hausaufgabenplaner/Bilder/Beschreibung/$path");
        echo "Successfully Uploaded";
    }


    mysqli_query($connect, $query) or die (mysqli_error($connect));
    mysqli_close($connect);

Solution

  • I uploaded some images over php on the server and they are uploaded on the PHP directory (I don't know why)

    Based on the code you provided, this is how you are uploading the image:

    $path = "Beschreibung_$id.png";
    file_put_contents($path,base64_decode($image));
    

    There is no directory specified, therefore the image will be written to the same directory as the PHP file currently running.

    now I want to move them in the "sister" directory Bilder and their "child" Beschreibung.

    Again, based on the code you provided:

    rename("$path", "/Hausaufgabenplaner/Bilder/Beschreibung/$path");
    

    You're only specifying the relative path, not the absolute path. So this will translate to: /Current Directory/Hausaufgabenplaner/Bilder/Beschreibung/$path

    You can find the absolute path by echoing $_SERVER['DOCUMENT_ROOT']