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 h
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
You didn't set the variable
$target_path
you meant but not used
$target_file
instead.
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.