Home  >  Q&A  >  body text

Unable to move upload file to /tmp

This is my uploads.php Code:

<?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 doesn't have the correct permissions, I get an error message.

/tmp directory as I guess the correct permissions are:

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

So why does it return true but not copy anything?

P粉092778585P粉092778585174 days ago272

reply all(1)I'll reply

  • P粉129275658

    P粉1292756582024-04-02 09:30:40

    If your tmp directory and the uploads.php file are changed at the same level:

    $uploads_dir = '/tmp' . DIRECTORY_SEPARATOR;
    

    Regarding:

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

    Or if the folder is at the root level (aka / ) You need to go down to the same level, for example:

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

    Hope it helps you.

    reply
    0
  • Cancelreply