SOLVED! File Upload via PHP Not Working

Hey guys,

I have searched on the forum for similar problems with uploading files via PHP, and have made the changes suggested in those topics. However, I am still experiencing issues. My setup has two parts: an HTML form and PHP script.

HTML Form


<form method="post" action="session_uploader.php" id="submit_session_scores">
    <table>  
    <tr><td align="right">Session Stats File:</td><td><font color="#4096EE"><input type="file" name="filename" id="filename" size="35" /></font></td></tr>
    <tr><td align="right">1 + 1 =</td><td><font color="#4096EE"><input type="text" name="test" id="test" class="contactform" size="10" /></font></td></tr>
    <tr><td align="right"><input type="submit" value="Submit" id="submitok" name="submitok" /></td><td><input type="reset" value="Clear Form" id="clear_form" /></td></tr>
      </table>
</form>

Session_Uploader.php Script
[php]

<?php //Check if the user input the correct test information (should == 2) $test = $_POST['test']; if($test == '2') { //Test was successful //Specify the location of the stat feeds $target = "statfeeds/"; $target = $target . basename($_FILES['filename']['name']); //$target should look something like "statfeeds/session_stats.csv"; //Move the file to the stat feeds folder if(move_uploaded_file($_FILES['filename']['tmp_name'], $target)) { echo "The file ". basename($_FILES['filename']['name']). " has been uploaded"; } else { echo "Sorry, there was a problem uploading your file..."; } } else { //Test was unsuccessful echo "Numerical test failed."; //header("location: submitscores.php"); //exit(); } ?>

[/php]

No matter what file the user attempts to upload, they receive the error: “Sorry, there was a problem uploading your file…” Therefore, I can see that the move_uploaded_file() test is failing.

What have I tried to rectify this issue?

[ul][li]I have set the CHMOD settings on the “statfeeds” directory to 777 (full read, write, and execute permissions). No success.[/li]
[li]I have set the CHMOD settings on the main directory containing both the HTML form and the PHP script to 777 (full read, write, and execute permissions). This results in an Internal 500 error from the servor, probably due to the HTML file attempting to load with incorrect directory permissions.[/li]
[li]I have tried setting $target to a static value, such as “statfeeds/session_stats.csv”. I then uploaded a file with the exact same name. No success.[/li]
[li]I have tried putting a value other than 2 in the test input box in the form. This results in the error: “Numerical test failed.” as expected. This shows that the numerical test is successful.[/li]
[li]I have contacted my hosting provider (Globat) who is looking into my server’s setup for any errors there. I haven’t heard back from the support team yet.[/li]
[li]I have tried banging my head into the keyboard a ridiculous number of times. This had little positive effect on the success of the script, but I did accidentally delete a song by the Jonas Brothers. Therefore, I conclude that this adjustment was marginally successful ;D .[/li][/ul]
I have seen this script used successfully on a countless number of other sites, so I may be missing something trivial here…any help or added insight you could provide would be greatly appreciated!

THANK YOU SO MUCH!!!

-Tom Winchester-

You need to add this attribute to your tag:

enctype="multipart/form-data"

so that your form tag looks like this:

<form enctype="multipart/form-data" method="post" action="session_uploader.php" id="submit_session_scores">

This should solve your problem. Also, in your php code you can use this function to check if file was uploaded, and then process it:

[php]

<?php if(is_uploaded_file($_FILES['filename']['tmp_name'])){ // move uploaded file here ... } else{ echo 'Please upload file!'; } ?>

[/php]

Thanks for the quick reply! That was exactly the issue. It figures that with all the work I put into debugging the PHP script, I never even looked at the HTML form! BAHHHH!!! >:(

Anyways, in case a future user stumbles on this topic, my completed code is below! THANKS FOR THE HELP!!!
-Tom Winchester-

HTML Form:


<form enctype="multipart/form-data" method="post" action="session_uploader.php" id="session_form">
    <table>  
    <tr><td align="right">Session Stats File:</td><td><font color="#4096EE"><input type="file" name="filename" id="filename" size="35" /></font></td></tr>
    <tr><td align="right">1 + 1 =</td><td><font color="#4096EE"><input type="text" name="test" id="test" class="contactform" size="10" /></font></td></tr>
    <tr><td align="right"><input type="submit" value="Submit" id="submitok" name="submitok" /></td><td><input type="reset" value="Clear Form" id="clear_form" /></td></tr>
   </table>
</form>

Session_Uploader.php:
[php]

<?php //Check if the user input the correct test information (should == 2) $test = $_POST['test']; if($test == '2') { //Numerical test was successful //Check if the user specified a file to upload if(is_uploaded_file($_FILES['filename']['tmp_name'])){ //Filename test was successful //Specify the location of the stat feeds $target = "statfeeds/session_stats.csv"; //Move the file to the stat feeds folder if(move_uploaded_file($_FILES['filename']['tmp_name'], $target)) { echo "The file ". basename($_FILES['filename']['name']). " has been uploaded. Redirecting in 5 seconds..."; header("refresh: 5; url=url_goes_here"); } else { echo "Sorry, there was a problem uploading your file. Redirecting in 5 seconds..."; header("refresh: 5; url=url_goes_here"); } } else { //Filename test was unsuccessful echo "Please upload a file! Redirecting in 5 seconds..."; header("refresh: 5; url=url_goes_here"); } } else { //Numerical test was unsuccessful echo "Numerical test failed. Redirecting in 5 seconds..."; header("refresh: 5; url=url_goes_here"); } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service