Jump to content

Archived

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

mstabrey

File upload

Recommended Posts

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

Share this post


Link to post
Share on other sites
[code]
<?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;
?>
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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..

[code]<?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;
?>[/code]

Edit: mistakes

Share this post


Link to post
Share on other sites

×

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.