File Upload script not working

Hello

I have a web page where I placed a File Upload box.
When the user selects a file from their computer, then clicks the ‘Upload’ button, my script is suppose to copy this file to my ‘Uploads’ directory
on the server. However it is not working and I can’t figure out
why.

The ‘Uploads’ directory on the server is located in the root directory
and has the appropriate permissions.

Any help would be appreciated.

UploadMe.php


<?php

   $sPath = 'http://eastcoasttechwriter.ca/wp-content/plugins/UploadMe/upload_image.php';

   print ("Click the <b>Browse</b> button to select a file to upload. Click the <b>Upload</b> button to copy the file to the server.<br><br>");

   print ("<form action='$sPath'  method='post' enctype='multipart/form-data'>");
   print ("<table border=0 width=400>");
   print ("<tr>");
   print ("   <td>");
   print ("   <label for='file'>Filename:</label>");
   print ("   </td>");
   print ("</tr>");

   print ("<tr>");
   print ("   <td>");
   print ("<input class='InputStyle' type='file' name='ufile' id='file'  size=50 maxlength=80 /> ");
   print ("   </td>");
   print ("</tr>");

   print ("<tr>");
   print ("   <td>");
   print ("   &nbsp;");
   print ("   </td>");
   print ("</tr>");

   print ("<tr>");
   print ("   <td>");
   print ("<input class='ButtonStyle' type='submit' name='submit' value=' Upload' /> ");
   print ("   </td>");
   print ("</tr>");
   print ("</table>");

   print (" </form>");

?>

upload_image.php


<?php

        $sTemp1          = $_FILES["ufile"]["name"];
        $sCopyFrom    = $_FILES["ufile"]["tmp_name"];
        $sCopyTo        = "/Uploads/" . $sTemp1;

        $myDirectory = opendir(".") or die("Unable to open directory.");

        $bValue = copy ($sCopyFrom, $sCopyTo);

        print ("<br>---$myDirectory--<br>$sCopyFrom  : $sCopyTo<br><br>");
        print ("--$bValue--<br>");
?>

Thanks.

Do you have the form attribute enctype = “multipart/form-data”?

Hi. Yes, the form tag has this attribute.

not entirely sure why you need all those print lines, but okay…
Try using [fphp]move_uploaded_file[/fphp] instead of copy. The uploaded file is not currently where you think it is.

Hi
I have changed my php script to use the move_uploaded_file and am still having
problems. Code below:

Below are the output from the variables $tmp_name and $name:
/tmp/phpANym21
Uploads/thumb_2storey_13.jpg

The variable $sDone returns ‘’.

<?php

   $uploads_dir = 'Uploads/';

    $tmp_name = $_FILES["ufile"]["tmp_name"];
    $name     = $uploads_dir  .  $_FILES["ufile"]["name"];

    print ("&lt;br&gt;$tmp_name&lt;br&gt;$name");

    $sDone = move_uploaded_file($tmp_name, $name);

    print ("&lt;br&gt;--$sDone--&lt;br&gt;");

?>

sDone should be set to either TRUE or FALSE. try running it through an IF.

Hi
I wrapped it in an IF statement as follows and it returned Not Set value.

  $sDone = move_uploaded_file($tmp_name, $name);

   if (! $sDone)
   {
         print ("&lt;br&gt;Not Set");
   }
  else
  {
         print ("&lt;br&gt;Set");
   }

The ‘Uploads’ directory on the server is located in the root directory

Hi Kath that is probably the base of your problem. Even when you have created a directory map Uploads in the httpdocs (or equal) it doesn’t mean it is in your website root. To make sure that you point the script in the right direction use $_SERVER[“DOCUMENT_ROOT”];

So then it should look like this:
$root=$_SERVER[“DOCUMENT_ROOT”];
$map=‘Uploads/’;

$uploads_dir = “$root”.“$map”;