-1

So i'm currently trying to create a code which simply creates and publishes a file to my webroot, modifies and writes to that file, and then finally change the location of the file to another directory/folder using move_uploaded_file()

This is my code so far

$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,'$dir/$title.txt');
fwrite($myfile, $_POST['textarea11']);
fclose($myfile);

The code doesn't work, i've tried echoing move_uploaded_file() and it returned nothing, however the file was uploaded but it's location just wasn't changed.

$dir is defined as $dir = __DIR__.'/../uploads/'; and $title is define as $title = $_POST['title'];

  • `move_uploaded_file()` works with files you upload through multipart forms. To move a local file between directories, you need to adapt a way discussed here: http://stackoverflow.com/questions/19139434/php-move-a-file-into-a-different-folder-on-the-server – Rehmat Feb 05 '16 at 17:32

2 Answers2

0

move_uploaded_file() can only be used if you are submitting a multipart form and you want to save the uploaded file.

What you probably need is this: http://php.net/manual/en/function.rename.php

Tim Hysniu
  • 1,446
  • 13
  • 24
0

Change your given code as

    $dir = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'uploads';
    $myfile = fopen($_POST['title'].".txt", "w");
    move_uploaded_file($myfile,"$dir".DIRECTORY_SEPARATOR."$title.txt");
    fwrite($myfile, $_POST['textarea11']);

In your code

move_uploaded_file($myfile,'$dir/$title.txt');

php variable $dir and $title value is not coming. and value of $dir is consisting '/' and you are adding one more to make full file path too.

Always use directory separator to run in all Operating System. some OS use '/' and some OS use '\'.

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109