PHP/File Directory/File Upload

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

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

<html>
<head>
<title>A file upload script</title>
</head>
<?php
$file_dir = ".";
$file_url = ".";
if ( isset( $fupload ) ){
   print "path: $fupload<br>\n";
   print "name: $fupload_name<br>\n";
   print "size: $fupload_size bytes<br>\n";
    print "type: $fupload_type<p>\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>



File Upload Global Variables

/*
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
*/



HTML form with a file input tag

<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"><br><br>
    <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>

<!--
<?php
    $aErrors = "";
    if ( !empty( $thefile_name ) ) // no file selected
    {
        if ( ( $thefile_type == "image/gif" ) ||
             ( $thefile_type == "image/pjpeg" ) ||
             ( $thefile_type == "image/jpeg" ) ){
            if ( $thefile_size < ( 1024 * 100 ) ){
                $aCurBasePath = dirname( $PATH_TRANSLATED );
                $aNewName = $aCurBasePath . $thefile_name;
                copy( $thefile, $aNewName );
            } else {
                $aErrors .= "The file was too big";
            }
        } else {
            $aErrors .= "The file was neither a gif nor a jpeg";
        }
    } else{
        $aErrors .= "No file was selected";
    }
?>
<html>
<head>
     <title>Display an Uploaded Image</title>
</head>
<body>
<?php
    if ( $aErrors != "" ){
        print( "<b>There were errors</b>: $aErrors<br>" );
    } else {
        print( "The picture you uploaded:<br><br>" );
        print( "<img src=\"/$thefile_name\" border=\"0\">" );
    }
?>
</body>
</html>
-->



Save Upload File to a new directory

<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">
   Last Name:<br /> <input type="text" name="name" value="" /><br />
   Homework:<br /> <input type="file" name="homework" value="" /><br />
   <p><input type="submit" name="submit" value="Submit Notes" /></p>
</form>
<?php
   define ("FILEREPOSITORY","./");
   if (isset($_FILES["homework"])) {
      if (is_uploaded_file($_FILES["homework"]["tmp_name"])) {
         if ($_FILES["homework"]["type"] != "application/pdf") {
            echo "<p>Homework must be uploaded in PDF format.</p>";
         } 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 "<p>File successfully uploaded.</p>";
            else 
               echo "<p>There was a problem uploading the homework.</p>";
         }
      }
   }
?>



Upload PDF file and rename it

<form action="<?php print $PHP_SELF?>" enctype="multipart/form-data" method="post">
   Last Name:<br /> <input type="text" name="name" value="" /><br />
   Class Notes:<br /> <input type="file" name="classnotes" value="" /><br />
   <p><input type="submit" name="submit" value="Submit Notes" /></p>
</form>
<?php
   define ("FILEREPOSITORY","./");
   if (is_uploaded_file($_FILES["classnotes"]["tmp_name"])) {
      if ($_FILES["classnotes"]["type"] != "application/pdf") {
         echo "<p>Class notes must be uploaded in PDF format.</p>";
      } else {
         $name = $_POST["name"];
         $result = move_uploaded_file($_FILES["classnotes"]["tmp_name"], FILEREPOSITORY."/$name.pdf");
         if ($result == 1) echo "<p>File successfully uploaded.</p>";
         else echo "<p>There was a problem uploading the file.</p>";
      } #endIF
   } #endIF
?>