Page 1 of 1

Attachments

Posted: Mon Jan 25, 2010 3:37 am
by Jay87
I have the following code on the page i want the attachments to be...

The table works fine and allows me to 'chose a file' and submit it...

Code: Select all

        <table width=800 height=50% class='smooth'>
 
        <tr><td class=smooth height=10%>Attachement(s)</td></tr>
        <tr><td><?echo $_FILES["file"]["name"] ?></td></tr>
        </table>
        
        
        <form action="upload_file.php" method="post"
            enctype="multipart/form-data">
            <label for="file">Filename:</label>
            <input type="file" name="file" id="file" /> 
            <br />
            <input type="submit" name="submit" value="Submit" />
            </form>
This code is suppose to re-direct the upload into the folder 'upload' from 'temp_file' (i think?), but after i upload something it never appears in the upload folder. Any ideas on where i am going wrong?

Code: Select all

<?php
if ($_FILES["file"]["size"] < 2000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
Also how can i show the files that have been attached on the page?

Re: Attachments

Posted: Mon Jan 25, 2010 5:02 am
by Jay87
I've changed the code to the following:

Code: Select all

<?php
$uploaddir = date("F")." '".date("y");
if ($_FILES["file"]["size"] < 2000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("upload/" . $uploaddir."/". $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      { mkdir("uploads/" . $uploaddir);
      move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $uploaddir."/". $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $uploaddir."/". $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
I get the following errors:

- PHP Warning: move_uploaded_file(upload/January '10/helpdesk-original2.jpg) [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: failed to open stream: No such file or directory in /var/www/helpdesk-dev/upload_file.php on line 22, referer: http://helpdesk:90/viewcall.php?id=0094 ... 10&sub=att


-PHP Warning: move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: Unable to move '/tmp/phpuHaKEF' to 'upload/January '10/helpdesk-original2.jpg' in /var/www/helpdesk-dev/upload_file.php on line 22, referer: http://helpdesk:90/viewcall.php?id=0094 ... 10&sub=att


:banghead:

Re: Attachments

Posted: Mon Jan 25, 2010 5:28 am
by whistler
As sleep deprived as I am, this is somewhat hilarious that we're working on the same bit of code.

I'm fairly sure that you need to check the permissions on the folder that you're trying to save the uploaded files to.

Try that, and I'll reread the changes you've made to the code.

Re: Attachments

Posted: Mon Jan 25, 2010 5:52 am
by Jay87
whistler wrote:As sleep deprived as I am, this is somewhat hilarious that we're working on the same bit of code.

I'm fairly sure that you need to check the permissions on the folder that you're trying to save the uploaded files to.

Try that, and I'll reread the changes you've made to the code.
Everything is ticked on the 'upload' folder, so it should allow for new files to be copied into it...!