phpmove-uploaded-file

php move_uploaded_file with absolute path


I have my project link as follows : http://localhost/prolist_admin/ and I use the move_uploaded_file as follows move_uploaded_file($temp_name, "../assets/img/static/$projectLogo");

The above code moves my file to the right folder, with the permission set to 777 on that folder.

Now, I want to move the files to a different folder, not the project folder.

for this, I'm having a hard time using ../../../ to navigate there( I don't think it's a good idea either)

Is there a way I can just use something like the following absolute linking http://localhost/newFolder/$projectLogo to move the files to a brand new folder, outside the project folder?

Thanks!


Solution

  • <?php
    
    // Supposing that the path is /mnt/c/Users/Dimitris/Desktop/projects/StackOverflow
    
    $path = realpath('./../../../Desktop'); // Desination path is my Desktop.
    
    if(isset($_POST['upload'])){
    
        $numberOfFiles = sizeof($_FILES["song"]["name"]);
        echo "<br>Number of selected files: ".$numberOfFiles.'<br>';
    
        for($i=0; $i<$numberOfFiles; $i++){
    
            $tempName = $_FILES['song']['tmp_name'][$i];
            $desPath = $path."/" . $_FILES['song']['name'][$i]; // Destination path is my Desktop
    
            if (file_exists(realpath(dirname(__FILE__))."/" . $_FILES['song']['name'][$i]))
            {
                echo $_FILES['file']['name'] . " already exists. ";
            }
            if(!move_uploaded_file($tempName, $desPath))
            {
                echo "File can't be uploaded";
            }
        }
    
    }
    
    ?>
    
    <form method="post" action="<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data">
        <input type="file" multiple="multiple" name="song[]" accept="audio/mpeg3"><br></br>
        <div class="uploadbtn" align="center">
        <input type="submit" name="upload" value="Upload"></div>
    </form>