Jump to content

Unable to Upload Files: Suggestion / Help needed


gcreddy

Recommended Posts

Hello, I am beginner in PHP. I have been using a dynamic form to upload files but it isn't working. The code is as follows:

 

<table border="0" cellpadding="5px">

<tbody><form id="form" class="form" enctype="multipart/form-data" method="post" action="" onsubmit="return validate(this)">

<tr><td>

<span style ="font-family:Arial">Click to add files</span>  

<input id="Button1" type="button" value="add" onclick = "AddFileUpload()" /></td></tr>

 

<tr><td><div id = "FileUploadContainer">

        <!--FileUpload Controls will be added here -->

    </div></td></tr>

 

<tr>

<td><input name="submit" type="submit" /></td>

</tr>

</table></form>

 

 

Javascript Code:

 

var counter = 0;

function AddFileUpload()

{

    var div = document.createElement('DIV');

    div.innerHTML = '<input id="file' + counter + '" name = "file' + counter +'" type="file"/>' + '<input id="Button' + counter + '" type="button" ' + 'value="Remove" onclick = "RemoveFileUpload(this)"/>';

    document.getElementById("FileUploadContainer").appendChild(div);

}

function RemoveFileUpload(div)

{

    document.getElementById("FileUploadContainer").removeChild(div.parentNode);

}

 

PHP Code:

 

  print_r($_FILES); // I used this line to see if any files were being posted at all and I found out that not even a single file was posted!

  $dir = "/var/www/vhosts/ncaquariums.com/httpdocs/photocontest09/".$_REQUEST["fname"]." ".$_REQUEST["lname"];

  $file_name = $_REQUEST["fname"]." ".$_REQUEST["lname"].".txt";

  $file_temp = $_REQUEST["fname"]." ".$_REQUEST["lname"]."tmp";

 

    if(@mkdir($dir, 0777))

    {

      $i = 0;

      $file = "file0";

      while($_FILES[$file]){

            print_r($_FILES[$file]);

            if ((($_FILES[$file]["type"] == "image/gif") || ($_FILES[$file]["type"] == "image/jpeg") || ($_FILES[$file]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 2097152))

            {

              $file_data = "First Name:".$_REQUEST["fname"]."\nMiddle Name:".$_REQUEST["mname"]."\nLast Name:".$_REQUEST["lname"]."\nMailing Address:".$_REQUEST["address"]."\nCity:".$_REQUEST["city"]."\nState:".$_REQUEST["state"]."\nZIP:".$_REQUEST["zip"]."\nCountry:".$_REQUEST["country"]."\nHome Phone:".$_REQUEST["home"]."\nMobile Phone:".$_REQUEST["mobile"]."\nEmail:".$_REQUEST["email"]."\nCategory:".$_REQUEST["category"]."\n";

              $fp = fopen($dir."/".$file_temp,w);

              fputs($fp, $file_data);

              fclose($fp);

              rename($dir.'/'.$file_temp,$dir.'/'.$file_name);

              move_uploaded_file($_FILES[$file]["tmp_name"],  $dir ."/". $_FILES[$file]["name"]);

            }

            else{

              echo "Sorry, your image(s) do not meet the specified requirements. Please try again.";

              exit;

            }

            $i++;

            $file = "file".$i;

        }

    }

 

 

I would be glad to accept any suggestions / inputs to know what is going wrong.

 

Regards,

G.C.Reddy

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.