-1

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!

youngP
  • 7
  • 4
  • 1
    _"Is there a way I can just use something like the following"_ - no, something like _that_ you can of course not use - because that is a URL, and not a file system path. Make yourself aware of the difference. – CBroe Oct 14 '22 at 08:13

2 Answers2

0
<?php
    // Supposing that the path is /mnt/c/Users/Dimitris/Desktop/projects/StackOverflow

    echo realpath('./../../../Desktop'); // goes to Desktop
    echo '<br>';

    // Create a file in Desktop
    $thefile = realpath('./../../../Desktop')."/test.txt";
    echo $thefile;

    if (file_exists($thefile)) {
        $fh = fopen($thefile, 'a');
        fwrite($fh, 'Test file');
    } else {
        $fh = fopen($thefile, 'wb');
        fwrite($fh, 'Test file');
    }

    fclose($fh);
    chmod($thefile, 0777);
?>

https://www.php.net/manual/en/function.realpath

D. Dimopoulos
  • 315
  • 2
  • 6
  • Hey mate, Thanks for this, and sorry for the late response! It actually echoed the right path, but I was still unable to move the uploaded file there. I don't know if I can't move to another brand new folder. – youngP Oct 13 '22 at 21:16
  • @youngP check the second answer, in which I have a full example to upload files in my Desktop. – D. Dimopoulos Oct 14 '22 at 13:52
0
<?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>
D. Dimopoulos
  • 315
  • 2
  • 6
  • Thanks, man! You've just saved me. I marked this post as "Answer" even though I don't have the needed reputation. Thanks main – youngP Oct 14 '22 at 22:14