Jump to content


Photo

File upload


  • Please log in to reply
3 replies to this topic

#1 mstabrey

mstabrey
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 May 2006 - 07:21 AM

I'm looking for some code to be able to upload files to a server. Have found something on this site, but it's way to in-depth for what I need. All I'm looking for is a single field for the file name with a browse button, and then an upload button. Nothing more than this ie. no validation etc.
Does something simple like this exist?

TIA

Mart

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 11 May 2006 - 08:39 AM

<?php
   if (!$file) {
      $form = "select a file to upload, biatch!<br>";
      $form.="<form action='$PHP_SELF' method='post' enctype='multipart/form-data'>";
      $form.="   <input type='file' name='file' size='50'><br>";
      $form.="   <input type='submit' value='upload file!'>";
      $form.="</form>";
   } elseif ($file_name !=" ") {
      copy ("$file","$file_name") or die("could not upload your shit, man");
      $form ="File upload succeeded...<br>";
      $form.="<ul><li>sent: ".$file_name;
      $form.="<li>size: ".$file_size;
      $form.="<li>type: ".$file_type;
   } else {
      $form = "unknown error.";
   }
   echo $form;
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 mstabrey

mstabrey
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 11 May 2006 - 12:44 PM

Wow thanks!

2 things:
1. Where do I specify a folder called "trailers" to upload to?
2. I get an error message "Warning: copy(asoutput.log): failed to open stream: Permission denied in /usr/www/users/muizenj/test.php on line 17" when I press the upload button. Is this related to my question 1 possibly.

Mart

#4 Carth

Carth
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts
  • LocationUK

Posted 12 May 2006 - 07:44 PM

The upload form uploads the file to a temporary directory, which is why [a href=\"http://www.php.net/copy\" target=\"_blank\"]copy()[/a] is used to transfer it to where you want it. The second parameter is where to copy to. Make sure this location exists, and is writeable.

If register_global isn't on, you can't use the $file or $file_name variable. You should use $_FILES['file']['tmp_name'] and $_FILES['file']['name'] etc..

<?php
   $location = "/path/to/trailers";
   if (!$_FILES['file']['tmp_name']) {
      $form = "select a file to upload, biatch!<br>";
      $form.="<form action='$PHP_SELF' method='post' enctype='multipart/form-data'>";
      $form.="   <input type='file' name='file' size='50'><br>";
      $form.="   <input type='submit' value='upload file!'>";
      $form.="</form>";
   } elseif($_FILES['file']['tmp_name'] != "none") {
      copy ($_FILES['file']['tmp_name'], $location.$_FILES['file']['name']) or die("could not upload your shit, man");
      $form ="File upload succeeded...<br>";
      $form.="<ul><li>sent: ".$_FILES['file']['name'];
      $form.="<li>size: ".$_FILES['file']['size'];
      $form.="<li>type: ".$_FILES['file']['type'];
   } else {
      $form = "upload failed";
   }
   echo $form;
?>

Edit: mistakes




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users