skip to Main Content

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!

2

Answers


  1. <?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

    Login or Signup to reply.
  2. <?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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search