user2480054
user2480054

Reputation: 537

Move_uploaded_file() function is not working

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>

Upvotes: 55

Views: 358462

Answers (18)

Vlad Miller
Vlad Miller

Reputation: 2279

  1. Enable PHP error reporting in order to see the error message from move_uploaded_file() that explains the problem.
  2. Check the $_FILES['image']['error'] variable.

In your case it's a wrong filename. The file will be stored in a temporary location, so use tmp_name instead of name:

move_uploaded_file($_FILES['image']['tmp_name'], __DIR__.'/../../uploads/'. $_FILES["image"]['name']);
// echo "Uploaded";

Upvotes: 69

Pablo Contreras
Pablo Contreras

Reputation: 588

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.

Upvotes: 0

codr
codr

Reputation: 1029

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

Upvotes: 0

Syed Waqas Bukhary
Syed Waqas Bukhary

Reputation: 5340

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.

Upvotes: 1

abubakkar tahir
abubakkar tahir

Reputation: 847

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";
}

Upvotes: 1

yenren
yenren

Reputation: 501

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.

Upvotes: 0

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>";
?>

Upvotes: 22

VOIZ
VOIZ

Reputation: 11

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

Upvotes: 1

Love Kumar
Love Kumar

Reputation: 1124

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);

Upvotes: -1

King Of The Jungle
King Of The Jungle

Reputation: 745

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/

Upvotes: 5

user889030
user889030

Reputation: 4754

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

Upvotes: 1

HexaCrop
HexaCrop

Reputation: 4263

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.

Upvotes: 2

user3502785
user3502785

Reputation: 337

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

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

Upvotes: 32

shivgre
shivgre

Reputation: 1173

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.

Upvotes: 4

Joe Cheng
Joe Cheng

Reputation: 9524

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

Upvotes: 6

chirag ode
chirag ode

Reputation: 960

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); 

Upvotes: 4

Hanky Panky
Hanky Panky

Reputation: 46900

$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.

Upvotes: 11

Garry
Garry

Reputation: 595

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.

Upvotes: 2

Related Questions