0

I'm trying to upload a file to my local server, but it keeps being unsuccessful.

All my files are inside /var/www/html/ However I made a folder called uploads in the html folder, and I changed its permissions to 777 (what I took on average from searching was the best for my needs)

this is my code: index.html

<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

upload.php

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
echo "Target File: " . $target_file . "<br />";

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).  " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>
10
  • 8
    Clue: Look at your input name and the other two in the $_FILES arrays. Look at those very carefully. You will find your own answer and would have performed your first debugging operation. ;-) Commented Jan 12, 2015 at 15:53
  • aside : there is very rarely any reason to ever 777 Commented Jan 12, 2015 at 15:57
  • @CD001 Probably thinking that if folder was set to 755 and that didn't work, then 777 "must" work then. Commented Jan 12, 2015 at 15:58
  • and why do you use a hidden field to pass MAX_FILE_SIZE ? Commented Jan 12, 2015 at 15:58
  • 1
    where is target path Commented Jan 12, 2015 at 16:09

3 Answers 3

1

Your Input file is

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

so change $_FILES['fileToUpload']['name'] to $_FILES['uploadedfile']['name']

$_FILES['uploadedfile']['name'] Must have the value of Name attribute of your file field

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

1 Comment

Cheers for answering! I appreciate it
0

You didn't set the variable

$target_path

you meant but not used

$target_file

instead.

2 Comments

Thanks for the reply! A tad embarrassing for that to be there. However i'm still getting the same unsuccessful upload
You are wrong. OP is using wrong Input Field name inside $_FILES['uploadedfile']['name'] See my answer
0

Try This:

index.html

 <!DOCTYPE html>
 <html>
 <body>
 <form enctype="multipart/form-data" action="upload.php" method="POST">
 Choose a file to upload: <input name="uploadedfile" type="file" /><br />
 <input type="submit" value="Upload File" />
 </form>
 </body>
 </html>

upload.php

if(isset($_FILES["uploadedfile"]["type"]) && ($_FILES["uploadedfile"]["size"] < 5000000)){
      $sourcePath = $_FILES['uploadedfile']['tmp_name'];
      $file = $_FILES['uploadedfile']['name'];
      $targetPath = "/uploads/".$file;
      if(move_uploaded_file($sourcePath,$targetPath)){
      echo "The file: ".$_FILES['uploadedfile']['name']." has been uploaded";
   }else{
      echo "Looks like it failed.";
   }
}else{
   echo "You forgot to select a file, or the file size is too large.";
}

So what this does is checks if a file exists and checks if it's smaller than 5MB. If so it moves on to the upload part.

3 Comments

Thanks for replying, but it just doesn't seem to work. It's just failing every time, regardless of the code
What kind of server are you running? @luna-games
@jasson-basset Thanks for the reply, I'm running a local apache server

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.