1

I am new to HTML and PHP. I used html/php code from https://gist.github.com/taterbase/2688850 to implement a file upload page on my web server. Created "uploads/" folder in the server and gave chmod 777 permission to it.

It is working when I use name="uploaded_file", the original, I can see the file in the folder. However it is failing when I change the name="xxx" to something else, such as another name or the array form of it, no message on screen, file is not seen in the uploads folder.

Is the "uploaded_file" a fix or hard-coded kind of value? My aim is to use an array instead later, to make it for multiple files upload, however this name change restriction doesn't let me.

Please see below working and not-working samples:

upload.php (name="uploaded_file") Works

<!DOCTYPE html>
<html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="upload.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>
<?PHP
  if(!empty($_FILES['uploaded_file']))
  {
    $path = "uploads/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>

upload.php (name="user_file") Doesn't work

<!DOCTYPE html>
<html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="upload.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="user_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>
<?PHP
  if(!empty($_FILES['user_file']))
  {
    $path = "uploads/";
    $path = $path . basename( $_FILES['user_file']['name']);
    if(move_user_file($_FILES['user_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['user_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>
2
  • Did you happen to do a global search / replace on s/uploaded_file/user_file? Commented May 9, 2018 at 4:44
  • Oh yes, I did, and this changed the built in function name as well, right? :) Thanks for pointing Commented May 9, 2018 at 4:57

3 Answers 3

1

This line has the problem move_user_file($_FILES['user_file']['tmp_name'], $path).

php's bool move_uploaded_file ( string $filename , string $destination ) moves the uploaded files to a desired location. It is a built in function in php's API.

Your code should be changed to

move_uploaded_file($_FILES['user_file']['tmp_name'], $path)

in order to get it to work.

Your misunderstanding I think has come from your naming. Previously your field name is uploaded_file and the function name is move_uploaded_file(). So you must have thought that, when your field name is user_file, you should call move_user_file() in php. That's kinda creative thinking.. :D

Is the "uploaded_file" a fix or hard-coded kind of value? Well, you need to read the link given.

Sign up to request clarification or add additional context in comments.

Comments

1

You are changing the core function call to either undefined function or a user defined function.

If you have defined a function move_user_file(), then its ok.

But, if you are expecting a core PHP inbuilt function, then there is no function move_user_file().

The line seems to have problem:

if(move_user_file($_FILES['user_file']['tmp_name'], $path)) {

Should be:

if(move_uploaded_file($_FILES['user_file']['tmp_name'], $path)) {

You are ending up in calling a undefined function move_user_file().

The required function is: move_uploaded_file()

Comments

1

Its not move_user_file, its move_uploaded_file. Next time, learn how to use your editor's find and replace correctly ;o)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.