7

I've attempted to write code to have a file uploaded to a "media" folder in PHP. For some reason it continues to not work.

Here's the execution code:

move_uploaded_file($_FILES["file"]["tmp_name"],"../media/" . $_FILES["file"]["name"]) or die ("Failure to upload content");

Here's my form code:

    <input type="file" name="file" id="file" /> 

Any ideas why it may not be working?


EDIT:

When I use the command "print_r($_FILES);", it displays:

Array ( [file] => Array ( [name] => Screen Shot 2012-05-29 at 12.36.11 PM.png [type] => image/png [tmp_name] => /Applications/MAMP/tmp/php/phpHNj3nW [error] => 0 [size] => 71640 ) )

Image is NOT uploaded into the folder.

11 Answers11

31

Make sure that in your form.. you put the enctype.
eg: <form method="post" enctype="multipart/form-data" action="index.php"></form>;

To check if files are successfully updated upon submitting the form. use print_r to see results.
print_r($_FILES);

  • when i did the "print_r($_FILES);", it displayed "Array ( )" –  May 29 '12 at 22:04
  • it means the files are not successfully uploaded.. use enctype="multipart/form-data" in your form so that you see the result of print_r($_FILES) upon form submission. – Michael Roewin Tan May 29 '12 at 22:05
  • Array ( [file] => Array ( [name] => Screen Shot 2012-05-29 at 12.36.11 PM.png [type] => image/png [tmp_name] => /Applications/MAMP/tmp/php/phpHNj3nW [error] => 0 [size] => 71640 ) ) –  May 29 '12 at 22:08
  • it works fine now.. continue with your work.. good luck.. Cheers :) – Michael Roewin Tan May 29 '12 at 22:15
  • I've never heard or seen having to specify the enctype of a html form. Suddenly my file upload is working. What is the condition that causes this? I know I've uploaded files without having to speciy this. – decapo May 09 '17 at 00:36
  • 1
    Don't forget set 777 permission for your upload/ directory. For ubuntu ```$sudo chmod -R 777 uploads/``` – Maxim Strutinskiy Nov 21 '17 at 22:27
6

make sure media folder has 777 permission and the path ../media/ is correct

mgraph
  • 15,238
  • 4
  • 41
  • 75
3

Put the encription in form

<form method="post" action="index.php" enctype="multipart/form-data"></form>

Check in php.ini file max_execution_time heavy images are not uploaded due to execution time..

SuReSh
  • 1,503
  • 1
  • 22
  • 47
2

Check in php.ini file max_execution_time heavy images are not uploaded due to execution time..

eg: ;

1

There is a form with encrypt type or an ajax call? Do you check if the file is sended to the upload script (with a print_r($_FILES["file"]). If correct, do you have check if the relative path is correct? You must start from the current script (if file is included you must start from the including script). Sorry if answer seems simply, but the posted code is a little too short to evaluate.

0

In your form tag you want something like this <form enctype="multipart/form-data" action="uploader.php" method="POST"> Make sure enctype is set to multipart/form-data for files. Replace uploader.php with the name of the php file doing the processing. Also make sure your permissions are set so the file can be created in the directory.

Here's a list of possible problems: http://php.net/manual/en/features.file-upload.php

Celeritas
  • 14,489
  • 36
  • 113
  • 194
0

Have you checked that the "web server user" has write permissions to "../media" ?

davidgo
  • 269
  • 4
  • 10
0

I was having the same problem. I am using ubuntu 18.04 and it was solved when i used this permission command on terminal. sudo chmod -R 777 /var/www/html/target_dir. ->I have apache2 web server and target_dir as Download so replace target_dir as per your destination directory.

Hektor
  • 1,845
  • 15
  • 19
awadhesh
  • 1
  • 1
0

Make sure that in your form.. you put the enctype.

move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success
tryingToLearn
  • 10,691
  • 12
  • 80
  • 114
0

If anyone wounder what's the simplest file php uploader this is the code I test it, it will upload the file to current folder

IT'S NOT SECURE

This code for learning purpose only, don't upload it to working environment.

<html>
    <body>
        <form method="post" enctype="multipart/form-data">
            <label for="file">Filename:</label>
            <input type="file" name="file1" id="file1" /> 
            <br />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
</html>
<?php
if(isset($_POST['submit'])) {
    if ($_FILES["file1"]["error"] > 0) {
        echo "Error: " . $_FILES["file1"]["error"] . "<br />";
} else {
 echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
 echo "Type: " . $_FILES["file1"]["type"] . "<br />";
 echo "Stored file:".$_FILES["file1"]["name"]."<br/>Size:".($_FILES["file1"]["size"]/1024)." kB<br/>";
 move_uploaded_file($_FILES["file1"]["tmp_name"],dirname(__FILE__).'/'.$_FILES["file1"]["name"]); 
  }
}
exit ();
?>

credit https://stackoverflow.com/a/15709181/3019002

Salem
  • 654
  • 7
  • 24
-1

You need to set the folder permission to 777. otherwise your file won't load

onur
  • 1