0

Here my code of uploads.php:

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

$uploads_dir = '/tmp' . DIRECTORY_SEPARATOR;
$tmp_name = $_FILES["file"]["tmp_name"];
$name = basename($_FILES["file"]["name"]);
echo move_uploaded_file($tmp_name, $uploads_dir . $name);

It returns 1 (aka true) but the file is not copied to /tmp. If I set $uploads_dir to a different directory with permissions 777 it works. If the target directory is wrong or does not have the right permissions I get an error message.

The /tmp directory as the correct permission I guess:

$ ls -l / | grep tmp
drwxrwxrwt  21 root root      20480 apr 21 17:39 tmp

so why it returns true but does not copy anything there?

Mark
  • 4,338
  • 7
  • 58
  • 120
  • You must specify the target file name as well `$uploads_dir = '/tmp' . DIRECTORY_SEPARATOR . 'filename';`. You are trying to replace the **directory** entry **/tmp** with the new file. – John Hanley Apr 21 '22 at 23:45
  • @JohnHanley sorry I don't understand. I contatenate `$upload_dir` with `$name` – Mark Apr 22 '22 at 10:13
  • You are specifying a directory name for the destination. You must specify a full path including the file name. – John Hanley Apr 22 '22 at 19:51

1 Answers1

0

If your tmp directory is in the same level of the file uploads.php change:

$uploads_dir = '/tmp' . DIRECTORY_SEPARATOR;

to this:

$uploads_dir = __DIR__ . '/tmp' . DIRECTORY_SEPARATOR;

Or if the folder it's in the root level (aka / ) You need to descend to the same level like:

$uploads_dir = __DIR__ . '/../../tmp' . DIRECTORY_SEPARATOR;

I hope it's help you.

  • Sorry but `/tmp` is an absolute path. It's the default temporary directory in Linux. I don't want a path relative to the server directories. – Mark Apr 21 '22 at 16:36
  • And Why you moving from the temporary files directory, if the file already in the temporary directory? Print in the screen the two variables: $_FILES and you $uploads_dir – Cassiano Mesquita Apr 21 '22 at 16:41
  • Because the initial temporary directory is under `/tmp/systemd-private-*` accessible only as `root`. The uploaded file should be accessible from other applications, but it's a temporary file so I don't need to move it to, say, my home folder. – Mark Apr 21 '22 at 16:44
  • So if /tmp is at a different level, try including it in open_basedir. – kissumisha Apr 21 '22 at 17:11