Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Help with PHP upload file code move_uploaded_file() fails.?
I have spent much of the day trying to get a simple PHP Upload program to work on my local PC. I cannot seems to figure what is wrong with the code as it keeps falling out with "here was an error uploading the file, please try again!" I am trying to create a simple file upload routine that allows the user to choose the target location. Save this code to a folder that has the following structure below it:
[localhost]
Conservation
Agendas
Minutes
================= CODE ===================
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
// Save this file to a folder that has two Subfolders of Conservation/Minutes and Conservstion/Agendas
// Trying to allow saving uploaded files to either of these folders.
if (!isset($_POST['submit'])) {
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="formexample.php" method="post">
<table>
<tr>
<th>First Name:</th>
<td><input maxlength="36" name="Fname" size="36" type="text"/</td>
</tr>
<tr>
<th>Last Name:</th>
<td><input maxlength="36" name="Lname" size="36" type="text"/></td>
</tr>
<tr>
<th style="vertical-align:top;">Document Type:</th>
<td>
Agenda:<input name="type" type="radio" value="Agendas" checked="checked"/>
Minutes:<input name="type" type="radio" value="Minutes"/>
</td>
</tr>
<tr>
<th>Board/Committee:</th>
<td>
<select name="boardcommittee">
<option value="Conservation">Conservation Committee</option>
</select>
</td>
</tr>
<tr>
<th>File to Upload:</th>
<td>
<input type="file" name="uploadedfile"/>
</td>
</tr>
<tr>
<td style="text-align:center;" colspan="2">
<input name="submit" type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
<?
} else {
// Get form data
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$type = $_POST["type"];
$boardcommittee = $_POST["boardcommittee"];
$file = $_POST["uploadedfile"];
// Where the file is going to be placed
$target_path = "/CM_DOCS/".$boardcommittee."/".$type."/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . $file;
// Show results
/**
* echo "Upload: " . $_FILES["uploadedfile"]["name"] . "<br />";
* echo "Type: " . $_FILES["uploadedfile"]["type"] . "<br />";
* echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . " Kb<br />";
* echo "Temp file: " . $_FILES["uploadedfile"]["tmp_name"] . "<br />";
* echo "Hello, ".$Fname." ".$Lname.".<br />";
* echo "You selected ".$type.", for ".$boardcommittee."<br/>";
* echo "File to upload is ".$file."<br/>";
* echo "In folder: ".$target_path."<br/>";
*/
// Save the file
/**
* if ((($_FILES["uploadedfile"]["type"] == "text/pdf")
* || ($_FILES["uploadedfile"]["type"] == "text/doc")
* || ($_FILES["uploadedfile"]["type"] == "text/docx"))
* && ($_FILES["uploadedfile"]["size"] < 100000))
* {
*/
print $target_path . "<br/>";
print $_FILES['uploadedfile']['tmp_name']."<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!";
die;
}
/**
* }
* else
* {
* echo "<br/><div style='color:red;'>Invalid File Type</div>";
* }
*/
}
?>
</body>
</html>
1 Answer
- Web ProgrammerLv 49 years agoFavorite Answer
You need to include enctype="multipart/form-data" in your form tag.
<form method=" " enctype="multipart/form-data">
Otherwise, the $_FILES scope is not available.