Move_uploaded_file() function is not working

PhpFile

Php Problem Overview


I'm working on a website and I want the user to be able to upload files. So I'm trying to learn how to do that. I researched and it said that I had to use the function move_uploaded_file(). I wrote the code just like it was on the example (changing the data), but it wouldn't work. Please help me, I'm new at these. Here's what I've done so far:

<!DOCTYPE html>
<html>
   <head>
   </head>
<body>
   <form action="upload_file.php" method="POST" enctype="multipart/form-data">
      <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
      <input type="file"name="file">
      <input type="submit">
   </form>
</body>
<html>

This is the upload_file.php:

<!DOCTYPE html>
<html>
  <head>
  <head>
     <body>
        <?php
          $move = "/Users/George/Desktop/uploads/";
          echo $_FILES["file"]['name']."<br>";
          echo $_FILES["file"]['tmp_name']."<br>";
          echo $_FILES["file"]['size']."<br>";
          echo $_FILES['file']['error']."<br>";
          move_uploaded_file($_FILES['file']['name'], $move);
        ?>
     <body>
<html>

Php Solutions


Solution 1 - Php

The file will be stored in a temporary location, so use tmp_name instead of name:

if (move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/../../uploads/'. $_FILES["image"]['name'])) {
    echo "Uploaded";
} else {
   echo "File not uploaded";
}

Solution 2 - Php

Try using copy() function instead of move_uploaded_file(). It worked for me.

copy($_FILES['file']['tmp_name'], $path);

Solution 3 - Php

This is a working example.

HTML Form :

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

PHP Code :

<?php        
    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo "<p>";

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Upload failed";
    }

    echo "</p>";
    echo '<pre>';
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    print "</pre>";
?>

Solution 4 - Php

$move = "/Users/George/Desktop/uploads/".$_FILES['file']['name'];

That's one.

move_uploaded_file($_FILES['file']['tmp_name'], $move);

That's two.

Check if the uploads dir is writeable

That's three.

Return Values

> Returns TRUE on success. > > If filename is not a valid upload file, then no action will occur, and > move_uploaded_file() will return FALSE. > > If filename is a valid upload file, but cannot be moved for some > reason, no action will occur, and move_uploaded_file() will return > FALSE. Additionally, a warning will be issued.

Look at return value of the function.

That's it.

Solution 5 - Php

maybe you need to grant more permissions to your files.

suppose your code are under /var/www/my_project

try chmod -R 777 /var/www/my_project

Solution 6 - Php

This answer is late but it might help someone like it helped me

Just ensure you have given the user permission for the destination file

sudo chown -R www-data:www-data /Users/George/Desktop/uploads/

Solution 7 - Php

try this

$ImageName = $_FILES['file']['name'];
$fileElementName = 'file';
$path = 'Users/George/Desktop/uploads/'; 
$location = $path . $_FILES['file']['name']; 
move_uploaded_file($_FILES['file']['tmp_name'], $location); 

Solution 8 - Php

You are not refering to the temporary location where the file is saved.

Use tmp_name to access the file.

You can always see what's getting posted using :

echo "<pre>"; 
print_r($_FILES);

If you see this files array you will have an better understanding and idea of what's going on.

Solution 9 - Php

it should like this

move_uploaded_file($_FILES['file']['tmp_name'], $move);

And you cannot move it anywhere in your system .youcan move it in only in your project directory which must be in htdocs or www depends on what you are using wampp ,lampp or vertrgo.

Solution 10 - Php

If you are on a windows machine, there won't be any problems with uploading or writing to the specified folder path, except the syntactical errors.

But in case of Linux users, there is a workaround to this problem, even if there are no syntactical errors visible.

First of all, I am assuming that you are using this in a Linux environment and you need to upload something to your project folder in the public directory.

Even if you are having the write and read access to the project folder, PHP is not handled by the end user. It is and can be handled by a www-data user, or group.

So in order to make this www-data get access first type in;

sudo chgrp "www-data" your_project_folder

once its done, if there is no write access to the following as well;

sudo chown g+w your_project_folder

That will do the trick in Linux.

Please, not that this is done in a Linux environment, with phpmyadmin, and mysql running.

Solution 11 - Php

if files are not moving this could be due to several reasons

  • check permissions that upload directory , make sure its permission is at least 0755.

> > find * -type d -print0 | xargs -0 chmod 0755 # for directories find * > > -type f -print0 | xargs -0 chmod 0666 # for files

  • make sure upload directory owner & group is not root , in that case your script will not be able to write anything there, so change it to admin or any non-root user.

> chown -R admin:admin public_html # will restore permission to admin for folder and files within it > chown admin:admin public_html # will restore permission to admin for folder only will skip files

  • check your tmp directory that its writable or not so open php.ini and check upload_tmp_dir = your temp directory path , make sure its writable.
  • try copy function instead of move_uploaded_file

Solution 12 - Php

The mistake that I missed was not putting "enctype=multipart/form-data" as the element of the form. Just wanted to remind people because it might be your mistake

Solution 13 - Php

If move_uploaded_file() is not working for you and you are not getting any errors (like in my case), make sure that the size of the file/image you are uploading is not greater than upload_max_filesize value in php.ini.

My upload_max_filesize value was 2MB on my localhost and I kept trying to upload a 4MB image for countless times while trying to figure out what the issue with move_uploaded_file() is.

Solution 14 - Php

always set folder directory properly for image otherwise image will not be upload check my code for image upload if you issue still there let me know will help you

if (move_uploaded_file($_FILES['profile_picture']['tmp_name'],'../images/manager/'. 
    $_FILES["profile_picture"]['name'])) {
      echo "Uploaded";
} else {
      echo "File not uploaded";
}

Solution 15 - Php

move_uploaded_file($_FILES['file']['tmp_name'], $path) moves the file, which means temp file/buffers will be cleared from original source. move_uploaded_file() is open_basedir aware. However, restrictions are placed only on the to path as to allow the moving of uploaded files in which from path may conflict with such restrictions.

copy($_FILES['file']['tmp_name'], $to_path) When a file is copied, a duplicate is made means temporary buffers(source) is not cleaned. The $to_path should support overwriting of existing files.

Solution 16 - Php

For me, php.ini "upload_max_filesize" was too small (updated now and it works).

Solution 17 - Php

if you're working on centos greater than 6, you could check for sestatus....

if setenforce is 'enforcing' this might have repercussions on this PHP function, try with something like is_writeable('upload_dir'); to check if it's a writing permissions matter, and you can edit sestatus with>

setenforce permissive

this is one of many possible situations, like i said.

Solution 18 - Php

Easiest Way to Uplaod a file

$FileName = $file_name;
$path = 'Users/Desktop/uploads/images'; 
$location = $path . $data['name']; 
move_uploaded_file($data['temp_name'],$location);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2480054View Question on Stackoverflow
Solution 1 - PhpVlad MillerView Answer on Stackoverflow
Solution 2 - Phpuser3502785View Answer on Stackoverflow
Solution 3 - PhpShankar Narayana DamodaranView Answer on Stackoverflow
Solution 4 - PhpHanky PankyView Answer on Stackoverflow
Solution 5 - PhpJoe ChengView Answer on Stackoverflow
Solution 6 - PhpKing Of The JungleView Answer on Stackoverflow
Solution 7 - Phpchirag odeView Answer on Stackoverflow
Solution 8 - PhpshivgreView Answer on Stackoverflow
Solution 9 - PhpGarryView Answer on Stackoverflow
Solution 10 - PhpJithinView Answer on Stackoverflow
Solution 11 - Phpuser889030View Answer on Stackoverflow
Solution 12 - PhpVOIZView Answer on Stackoverflow
Solution 13 - PhpEnvayoView Answer on Stackoverflow
Solution 14 - Phpabubakkar tahirView Answer on Stackoverflow
Solution 15 - PhpSyed Waqas BukharyView Answer on Stackoverflow
Solution 16 - PhpcodrView Answer on Stackoverflow
Solution 17 - PhpPablo ContrerasView Answer on Stackoverflow
Solution 18 - PhpLove KumarView Answer on Stackoverflow