Zend - The PHP Company




File Uploading

Add Code


Lester Joseph Uploader  

Type: code fragment
Added by: phpbird
Entered: 01/11/2002
Last modified: 01/11/2001
Rating: - (fewer than 3 votes)
Views: 6637
File uploading based on one mime type and blocking all others. I used this script to only allow the uploading of pdf files. Please repale the directory names with your own.


###############This is the Upload form;####################

<html>
<head>
<title>Stats Digest Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<form action="upload.php" enctype="multipart/form-data" method="post">
<input type="file" name="statfile"><br><br>
<input type="submit" value="upload File"><br>
</form>
</body>
</html>
############################################################

############This is the upload script#######################
<?php
// this is the directory that the uploaded Statistics Digest will be kept in.
$dirname "statdigest";

//this is the full path of the Uploaded Statistic Digest
define(PATH,"Path to Directoy");

//gets the filename and extension from the file input in the form
$filename $statfile_name;
$accept_mtype "application/pdf";
$m_type =  $HTTP_POST_FILES['statfile']['type'];
/*Checks whether input file exists  & checks to see whether the file is a pdf
file. It only uploads PDF files
*/
if (($filename !="") && ($m_type == "application/pdf" ))
{

   
//checks to see whether the upload directory exists, if not, creates it
   
if (!$dir = @opendir(PATH.$dirname))
       
mkdir(PATH.$dirname,0700) or die
       (
"Could not create directory");

   
//copy the uploaded file from the temporary dir to the statdigest dir.
   
copy($statfile,PATH.$dirname."/".$statfile_name) or die
   (
"Could not copy  file");
   
unlink($statfile) or die
   (
"Could not delete uploaded file");
   echo 
"File Uploaded Successfully";

}

elseif (
$filename ==""){
        echo 
"You must  Select a File to upload";
        }

else {
echo 
"You are only allowed to upload PDF files";
}
?>
<html>
<head>
<title>Successful File Upload!!</title>
<body>
<P>You Sent: <? echo $_FILES['statfile']['name'?>, a <? echo $_FILES['statfile']['size'];?>
byte file with a mime type of <? echo $m_type ?>.</p>
</body>
</html>
############################################################


Usage Example




Rate This Script





Search



This Category All Categories