How to create File upload area on webpage

I am using Dreamweave and I would like to know what code I would use to allow users to upload files (Such as mp3, jpg, gif) from a webpage to my server.  I am running IIS from my home.  This is where the site is hosted.  I have read some solutions to similiar questions and have no luck.  I am more of a designer and not a coder so if you could bring me through one step at a time that would be great.  I have seen some php and asp code on this.  
LVL 1
SamBreck321Asked:
Who is Participating?
 
syedasimmeesaqConnect With a Mentor Commented:
ok you will need to do it in few steps.

First make a folder called uploaded in the directory where you are going to put the upload scripts. Make sure you give uploaded a write permission as it will store the files that have been uploaded by your users.

Then you will make    pages.

Page1: call it uploadform1.php

copy and paste this code between XXXXXXXX  into the file
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<?php
       
                 echo"<form name='form1' method='post' action='uploadForm2.php'>
  <p>Enter the amount of files you will need to upload. Max = 9 at a time.</p>
  <p>
    <input name='uploadNeed' type='text' id='uploadNeed' maxlength='1'>
  </p>
  <p>
    <input type='submit' name='Submit' value='Submit'>
  </p>
</form>";
?>      

XXXXXXXXXXXXXXXXXXXXXXXxx
The uploadform1.php is just asking users how many files they want to upload

now make another page called  uploadForm2.php and paste this code there
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
<form name="form1" enctype="multipart/form-data" method="post" action="processFiles.php">
  <p>
  <?
  // start of dynamic form
  $uploadNeed = $_POST['uploadNeed'];
  for($x=0;$x<$uploadNeed;$x++){
  ?>
    <input name="uploadFile<? echo $x;?>" type="file" id="uploadFile<? echo $x;?>">
  </p>
  <?
  // end of for loop
  }
  ?>
  <p><input name="uploadNeed" type="hidden" value="<? echo $uploadNeed;?>">
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
</div>
</div>
<div id="bttmbar"><? include('footer.php'); ?></div><!-- end of bttmbar -->

</body>
</html>
  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
And finaly make the last file call processfiles.php
and paste the code in snippet there
<?
 
$uploadNeed = $_POST['uploadNeed'];
// start for loop
for($x=0;$x<$uploadNeed;$x++){
 
 
$file_name = $_FILES['uploadFile'. $x]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$filename = "uploaded/".$file_name;
if (file_exists($filename)) {
$FileExist = str_replace("uploaded/","",$file_name);
    echo "<font color=yellow>The file <font color=red>$FileExist</font> already exists</font>";
} else {
 
$file_name = "uploaded/".$file_name; 
$FileInfo = explode(".",$file_name);
if($FileInfo[1] == "jpg" || $FileInfo[1] == "gif" || $FileInfo[1] == "doc" || $FileInfo[1] == "xls" || $FileInfo[1] == "xlsx"){
//this is a pdf file
 
$copy = copy($_FILES['uploadFile'. $x]['tmp_name'],$file_name);
 // check if successfully copied
 if($copy){
 $Filemessage = str_replace("uploaded/","",$file_name);
 echo "<font color=red> $Filemessage | uploaded sucessfully!<br></font>";
 //$linkedfile = "<a href='http://yourwebsite.com/path/to/your/directory/where/all/thefiles/are/uploads/".$file_name."'> $filename</a>";
 $linkedfile = "'http://yourwebsite.com/path/to/your/directory/where/all/thefiles/are/uploads/".$file_name;
 $user = $rownum['username'];
 //echo $linkedfile;
 echo"<br>";
 echo $user;
 $fileinfo = "insert into files (user, linkedfile, filedated) Values('".$user."','".$linkedfile."',CURRENT_DATE)";
 $resultinsert = mysql_query($fileinfo) or die(mysql_error());
 }else{
 echo "<font color=red>$Filemessage | could not be uploaded!<br></font>";
 }
 }else {echo"<font color=red>only certain kinds are allowed</font>";}
} // end of loop
}
?>

Open in new window

0
 
hieloConnect With a Mentor Commented:
Here's a step-by-step tutorial
http://php.about.com/od/advancedphp/ss/php_file_upload.htm

Save "step1" of tha page as upload.html

Save "step5" of that page as upload.php.

Below is a modified version of "step5" so you can easily provide custom file extensions. The folder that you upload to should be writable.
<?php
// specify the folder where the file should be uploaded. The setting 
// below will upload the file to http://www.yoursite.com/uploads/
$target = "/uploads/";
 
//specify a comma separated list of allowed files. No spaces in between
$allowedFiles="mp3,jpg,gif";
 
//specify size in bytes
$maxFileSize=350000;
 
 
// DO NOT CROSS THIS LINE :)
 
//used to monitor upload "success"
$ok=1;
 
// construct the absolute path for the uploaded file
$target = $target . basename($_FILES['uploaded']['name']);
 
//convert file list to an array
$allowedFiles = explode(",",$allowedFiles);
 
//This is our size condition
if ($uploaded_size > $maxFileSize)
{
	echo "Your file is too large.<br>";
	$ok=0;
}
 
//This is our limit file type condition
for($i=0, $limit=count($allowedFiles); $i < $limit; ++$i)
{
	if( $uploaded_type == $allowedFiles[$i] )
	{
		echo "No PHP files<br>";
		$ok=0;
	}
}
 
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
	Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
	if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
	{
		echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
	}
	else
	{
		echo "Sorry, there was a problem uploading your file.";
	}
}
?>

Open in new window

0
 
SamBreck321Author Commented:
Ok I tried it and I get a page with this:
HTTP 405 - Resource not allowed
Internet Information Services
Do all 3 files have to be in the "Uploaded folder"?
Also in the code for the "processfiles" I changed lines 28 & 29 with the correct path of the folder I want to use.  Is this correct?
0
 
hieloCommented:
>>HTTP 405 - Resource not allowed
This would happen if your the value of the form's action attribute is set to a file type that does not process data. For example, if it is currently coded as:
<form enctype="multipart/form-data" action="upload.html" method="POST">
and your server is not configured to allow html files to process information

OR perhaps
<form enctype="multipart/form-data" action="" method="POST"> or
<form enctype="multipart/form-data" method="POST">
and the page where this form is served from has a name ending in .html; if that is the case, when the form is sumbitted, the data will be sent to the server so that this same page process the information but since it is html and html pages are not configured to process/receive info, you will get the 405 error.

The fix is quire simple, set the action attribute to a file type that is allowed to do processing:
<form enctype="multipart/form-data" action="upload.php" method="POST">

I provided a lot of comments on my script so that you can easily upload the necessary file types and/or modify the code as necessary.
0
 
SamBreck321Author Commented:
I still get the error message.  The fix up top is what I already have in the code
<form enctype="multipart/form-data" action="upload.php" method="POST">
The action was set like this originally action="upload.php"
I am using IIS to serve the website.  The target folder I am listing as this:
<?php
// specify the folder where the file should be uploaded. The setting
// below will upload the file to http://www.yoursite.com/uploads/
$target = "http://12.223.23.213/VirtualDir/uploaded";
Is this correct?
Also in ISS I have checked the box to "Write" under the properties of the website.
0
All Courses

From novice to tech pro — start learning today.