zDoude
zDoude

Reputation: 7

I am having trouble moving a file to a folder in php

This is the code I am using

try
{
    // Execute the query to create the user
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
    $elliot =  $_POST['username'];
    $usor =  $elliot.php;
    $myfile = fopen("$elliot.php", "c+") or die("Unable to open file!");
    $txt = "Userpage\n";
    fwrite($myfile, $txt);
    fclose($myfile);
    rename('$usor', 'usors/$usor');
}

Essentially what I am doing is creating a user. When the user is created then what I want to code to do is make a userpage. Then I want the user page to be move to a folder called usors.

My problem is I can create the user page. But I am unable to move the newly created userpage.

Upvotes: 1

Views: 31

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Firstly, variables do not get parsed in single quotes.

rename('$usor', 'usors/$usor');

You need to use double quotes

rename("$usor", "usors/$usor");

Sidenote: "usors/$usor" will only be accessible if you are running your script just outside the usors folder. Make sure the path is correctly set.

Then this $usor = $elliot.php; you need to remove the .php from there.

$usor =  $elliot;

Since you are referencing $elliot = $_POST['username'];

And of course, making sure that proper permissions are set.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Seeing that you are using a POST array, make sure the form you are using is using a POST method and that the input for it bears the matching name attribute.

Upvotes: 1

Mat
Mat

Reputation: 2154

You could use the rename php function (http://php.net/manual/en/function.rename.php) but be sure the user running your php script (apache ?) has enough rights.

Upvotes: 0

Related Questions