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"] . " "; * echo "Type: " . $_FILES["uploadedfile"]["type"] . " "; * echo "Size: " . ($_FILES["uploadedfile"]["size"] / 1024) . " Kb "; * echo "Temp file: " . $_FILES["uploadedfile"]["tmp_name"] . " "; * echo "Hello, ".$Fname." ".$Lname.". "; * 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['upload… $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>
-
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.
tadds at Yahoo! Answers Visit the source
Related Q & A:
- How to upload file to google cloud storage using Java?Best solution by Stack Overflow
- How to upload file using ajax php?Best solution by Stack Overflow
- How to upload file in AngularJS?Best solution by Stack Overflow
- How to upload file to a server from Salesforce?Best solution by help.salesforce.com
- Help, how to upload videos on youtube?Best solution by support.google.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.