PHP/File Directory/File Upload

Материал из Web эксперт
Перейти к: навигация, поиск

A File Upload: path, file name, size and type

   <source lang="html4strict">

<html> <head> <title>A file upload script</title> </head> <?php $file_dir = "."; $file_url = "."; if ( isset( $fupload ) ){

  print "path: $fupload
\n"; print "name: $fupload_name
\n"; print "size: $fupload_size bytes
\n";
print "type: $fupload_type

\n\n"; if ( $fupload_type == "image/gif" ){ copy ( $fupload, "$file_dir/$fupload_name") or die ("Couldn"t copy"); print "<img src=\"$file_url/$fupload_name\"><p>\n\n"; } } ?> <body> <form enctype="multipart/form-data" action="<?php print $PHP_SELF?>" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="51200"> <input type="file" name="fupload"> <input type="submit" value="Send file!"> </form> </body> </html> </source>

File Upload Global Variables

   <source lang="html4strict">

/* Variable Name Contains Example


$fupload Path to temporary variable /tmp/phptemp $fuploadname Name of uploaded file yourfile.gif $fuploadsize Size (in bytes) of uploaded file 835 $fupload type MIME type of uploaded file image/gif

  • /
      </source>
   
  


HTML form with a file input tag

   <source lang="html4strict">

<html> <head>

    <title>Upload Form</title>

</head> <body> <form action="UploadSingle.php" method="post" enctype="multipart/form-data">

   Upload a file: <input type="file" name="thefile">

<input type="submit" name="Submit" value="Submit">

</form> </body> </html>


      </source>
   
  


Save Upload File to a new directory

   <source lang="html4strict">

<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">

  Last Name:
<input type="text" name="name" value="" />
Homework:
<input type="file" name="homework" value="" />
<p><input type="submit" name="submit" value="Submit Notes" />

</form> <?php

  define ("FILEREPOSITORY","./");
  if (isset($_FILES["homework"])) {
     if (is_uploaded_file($_FILES["homework"]["tmp_name"])) {
        if ($_FILES["homework"]["type"] != "application/pdf") {
echo "

Homework must be uploaded in PDF format.

";
        } else {
           $today = date("m-d-Y");
           if (! is_dir(FILEREPOSITORY.$today)) {
              mkdir(FILEREPOSITORY.$today);
           }
           $name = $_POST["name"];
           $result = move_uploaded_file($_FILES["homework"]["tmp_name"], FILEREPOSITORY.$today."/"."$name.pdf");
           if ($result == 1) 
echo "

File successfully uploaded.

";
           else 
echo "

There was a problem uploading the homework.

";
        }
     }
  }

?>

      </source>
   
  


Upload PDF file and rename it

   <source lang="html4strict">

<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">

  Last Name:
<input type="text" name="name" value="" />
Class Notes:
<input type="file" name="classnotes" value="" />

<input type="submit" name="submit" value="Submit Notes" />

</form> <?php

  define ("FILEREPOSITORY","./");
  if (is_uploaded_file($_FILES["classnotes"]["tmp_name"])) {
     if ($_FILES["classnotes"]["type"] != "application/pdf") {
echo "

Class notes must be uploaded in PDF format.

";
     } else {
        $name = $_POST["name"];
        $result = move_uploaded_file($_FILES["classnotes"]["tmp_name"], FILEREPOSITORY."/$name.pdf");
if ($result == 1) echo "

File successfully uploaded.

"; else echo "

There was a problem uploading the file.

";
     } #endIF
  } #endIF

?>


      </source>