PDA

View Full Version : <input type="file"> help!



Zaub
02-08-2008, 12:36 PM
I am making a form at the moment, and I have a <input type="file"> area there and I'm wondering how to get the items that people put it there to upload to a certain folder on my server.

Help is much appreciated!

Chris

Excellent
02-08-2008, 12:38 PM
Use an uploader?

Zaub
02-08-2008, 12:46 PM
What I am doing is it's for DJ Applications, and I want their DJ example clip which they are uploading to appear in a folder.

Excellent
02-08-2008, 12:56 PM
<?php
echo "<center>"; //We center the page.
$sitename = "sitename.com"; //Put your site url here.
switch ($_GET[page]) //We use the switch function to get the page.
{
default: //Self explanatory
echo "<form action='?page=upload' enctype='multipart/form-data' method='POST'>
Upload:
<input type='file' size='7' name='image' id='image'><br>
<input type='submit' name='submit' value='Upload'></form>";
break; //Break upload
case 'upload': //The upload case
$filename = $_FILES["img"]["name"];
$tmp_name = $_FILES["img"]["tmp_name"];
$img = "images/$filename"; //You can edit the images piece to where your images will be displayed
if (move_uploaded_file($tmp_name, "images/".$filename)) { //And here - we also move the file
echo "Image upload complete!<br><img src='$img'><br>
Forum ******?<br>
<input type='text' name='image' size='30' value='www.".$sitename.".com/files/upload/".$img."'>"; // We display that their file has been uploaded, show then the url and show them a picture of their upload
} else { //Lol the upload failed
die ("Don't bother uploading ever again.. seriously."); //True comment
}

break; //Break page

}
?>
Use that, change it round if you wish.

NN-Dom
02-08-2008, 01:01 PM
Try this ;)
Make a folder called djtrials in the same directory as this and it will work a treat :)
At the bottom, it also shows the users OWN uploaded files only, provided you do trialup.php?djname=DJNAMEHERE
name this file: trialup.php
to make each users trial seperate, do trialup.php?djname=DJNAMEHERE
and it will create a folder for each different DJNAMEHERE :)
its also a good way to store all their stuff :D
To allow more file types, just add more files to the array $FILE_EXTS :)

I basically copied my user upload system but edited it for you:)
Goodluck.
PS: the maxfile size is set at 10mb - ample amount :)

<?php
$MAX_SIZE = 10000000;
$person = $_GET['djname'];
$FILE_EXTS = array('.mp3','.wma','.asx');
$DELETABLE = false;
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "djtrials/".$person."/";
$upload_url = $url_dir."/djtrials/".$person;
if (!is_dir("djtrials/".$person."")) {
if (!mkdir($upload_dir))
die ("Error 1 happend!");
if (!chmod($upload_dir,0755))
die ("Error 2 happend!");
if (!chmod($upload_dir,0777))
die ("Error 3 happend!");
}
if ($_FILES['userfile']) {
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
echo "The file size is over ".$MAX_SIZE." Bytes.";
//File Extension Check
else if (!in_array($file_ext, $FILE_EXTS))
echo "<center>Sorry, the file ".$filename($file_type)." Is not allowed to be uploaded!</center>";
else
do_upload($upload_dir, $upload_url);

}
else if (!$_FILES['userfile']);
else
echo "<center>Invalid file specified.</center>";
/************************************************** **********
* List Files
************************************************** **********/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir/$file' rel=\"lightbox\" title=\"".$file."-Uploaded by ".$_SESSION['myusername']."\" rel=\"lightbox[".$_SESSION['myusername']."'s Uploads]\"\">".$file."</a>";
if ($DELETABLE)

$filelist .= " Added at ".date("d-m H:i", filemtime($upload_dir.$file))
."";
$filelist .= "";
$filelist .="<br>";
}
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
function getnumbertrial() {
$file_name = $person.time();.$_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="") {
echo "Invalid File Name Specified";
}

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
echo ($result)?"<center>$file_name WAS uploaded successfully, ".$name."!</center>" :
" <center>There was an unknown error whilst uploading the file.</center>";
}

?>
<center><form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>
</center>
<br><b><u>YOUR Uploaded files:</u></b><br><br>
<?=$filelist?>

Zaub
03-08-2008, 02:04 AM
Erm..

I get this error with the above method.


Parse error: syntax error, unexpected '.' in /home/chrism/public_html/habbohut/apps/trialup.php on line 56

Excellent
03-08-2008, 02:26 AM
Replace the . on line 56 with a ,

Zaub
03-08-2008, 02:29 AM
Replace the . on line 56 with a ,

Then it has an unexpected ',' now.

NN-Dom
03-08-2008, 09:41 AM
hah my mistake, sorry - here you go :D


<?php
$MAX_SIZE = 10000000;
$person = $_GET['djname'];
$FILE_EXTS = array('.mp3','.wma','.asx');
$DELETABLE = false;
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "djtrials/".$person."/";
$upload_url = $url_dir."/djtrials/".$person;
if (!is_dir("djtrials/".$person."")) {
if (!mkdir($upload_dir))
die ("Error 1 happend!");
if (!chmod($upload_dir,0755))
die ("Error 2 happend!");
if (!chmod($upload_dir,0777))
die ("Error 3 happend!");
}
if ($_FILES['userfile']) {
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
echo "The file size is over ".$MAX_SIZE." Bytes.";
//File Extension Check
else if (!in_array($file_ext, $FILE_EXTS))
echo "<center>Sorry, the file ".$filename($file_type)." Is not allowed to be uploaded!</center>";
else
do_upload($upload_dir, $upload_url);

}
else if (!$_FILES['userfile']);
else
echo "<center>Invalid file specified.</center>";
/************************************************** **********
* List Files
************************************************** **********/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir/$file' rel=\"lightbox\" title=\"".$file."-Uploaded by ".$_SESSION['myusername']."\" rel=\"lightbox[".$_SESSION['myusername']."'s Uploads]\"\">".$file."</a>";
if ($DELETABLE)

$filelist .= " Added at ".date("d-m H:i", filemtime($upload_dir.$file))
."";
$filelist .= "";
$filelist .="<br>";
}
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
function getnumbertrial() {
$file_name = $person.time();.$_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="") {
echo "Invalid File Name Specified";
}

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
echo ($result)?"<center>$file_name WAS uploaded successfully, ".$name."!</center>" :
" <center>There was an unknown error whilst uploading the file.</center>";
}

?>
<center><form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>
</center>
<br><b><u>YOUR Uploaded files:</u></b><br><br>
<?=$filelist?>

L?KE
03-08-2008, 08:42 PM
hah my mistake, sorry - here you go :D


<?php
$MAX_SIZE = 10000000;
$person = $_GET['djname'];
$FILE_EXTS = array('.mp3','.wma','.asx');
$DELETABLE = false;
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$upload_dir = "djtrials/".$person."/";
$upload_url = $url_dir."/djtrials/".$person;
if (!is_dir("djtrials/".$person."")) {
if (!mkdir($upload_dir))
die ("Error 1 happend!");
if (!chmod($upload_dir,0755))
die ("Error 2 happend!");
if (!chmod($upload_dir,0777))
die ("Error 3 happend!");
}
if ($_FILES['userfile']) {
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
echo "The file size is over ".$MAX_SIZE." Bytes.";
//File Extension Check
else if (!in_array($file_ext, $FILE_EXTS))
echo "<center>Sorry, the file ".$filename($file_type)." Is not allowed to be uploaded!</center>";
else
do_upload($upload_dir, $upload_url);

}
else if (!$_FILES['userfile']);
else
echo "<center>Invalid file specified.</center>";
/************************************************** **********
* List Files
************************************************** **********/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<a href='$upload_dir/$file' rel=\"lightbox\" title=\"".$file."-Uploaded by ".$_SESSION['myusername']."\" rel=\"lightbox[".$_SESSION['myusername']."'s Uploads]\"\">".$file."</a>";
if ($DELETABLE)

$filelist .= " Added at ".date("d-m H:i", filemtime($upload_dir.$file))
."";
$filelist .= "";
$filelist .="<br>";
}
}

function do_upload($upload_dir, $upload_url) {

$temp_name = $_FILES['userfile']['tmp_name'];
function getnumbertrial() {
$file_name = $person.time();.$_FILES['userfile']['name'];
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
$file_path = $upload_dir.$file_name;
//File Name Check
if ( $file_name =="") {
echo "Invalid File Name Specified";
}

$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
echo ($result)?"<center>$file_name WAS uploaded successfully, ".$name."!</center>" :
" <center>There was an unknown error whilst uploading the file.</center>";
}

?>
<center><form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File <input type="file" id="userfile" name="userfile">
<input type="submit" name="upload" value="Upload">
</form>
</center>
<br><b><u>YOUR Uploaded files:</u></b><br><br>
<?=$filelist?>

That was nice of you ;) +rep.

HostMania
07-08-2008, 04:26 PM
Here is the one i used for www.host-mania.com/upload (http://www.host-mania.com/upload)

Feel free to use what you need.


<?php
$uploadLocation = "";
?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>Host-Mania :: Uploader</title>

<link href="style/style.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div>
<div align="center"><span class="style1"><img src="logo.gif" alt="" width="500" height="141" /></span></div>
</div>
<div id="main">

<div id="caption">UPLOAD FILE</div>

<div id="icon">&nbsp;</div>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="fileForm" id="fileForm" enctype="multipart/form-data">

File to upload:<center>

<table>

<tr><td><input name="upfile" type="file" size="36"></td></tr>

<tr><td align="center"><br/><input class="text" type="submit" name="submitBtn" value="Upload"></td></tr>

</table></center>

</form>

<?php

if (isset($_POST['submitBtn'])){



?>

<div id="caption">RESULT</div>

<div id="icon2">&nbsp;</div>

<div id="result">

<table width="100%">

<?php



$target_path = $uploadLocation . basename( $_FILES['upfile']['name']);


if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)) {

echo "The file: ". basename( $_FILES['upfile']['name']).

" has been uploaded! And it is located here: <a href='http://www.host-mania.com/upload/" . $_FILES['upfile']['name'] . "' target='_blank'>" . "http://www.host-mania.com/upload/" . $_FILES['upfile']['name'] . "</a>";

echo "<br><br>Here is a code to show the image: <br>" . "<textarea name='textarea' cols='40' rows='4' wrap='VIRTUAL'><a href='http://www.host-mania.com/upload/" . $_FILES['upfile']['name'] . "' target='_blank'><img src='" . "http://www.host-mania.com/upload/" . $_FILES['upfile']['name'] . "'/></a></textarea>";

} else{

echo "There was an error uploading the file, please try again!";

}



?>

</table>

</div>

<?php

}

?>

<div>
<div>
<div align="center">Copyright 2008 Host-Mania.com</div>
</div>
<br />
<div align="center">
<!-- Begin BidVertiser code -->
<SCRIPT LANGUAGE="JavaScript1.1" SRC="http://bdv.bidvertiser.com/BidVertiser.dbm?pid=102223&bid=386020" type="text/javascript"></SCRIPT>
<noscript>
<a href="http://www.bidvertiser.com">make money</a>
</noscript>
<!-- End BidVertiser code -->
</div><br />
</body> You should just be able to replace the text that says host-mania. As is uses $_SERVER['PHP_SELF'] for the form!

Ryzie
18-08-2008, 08:33 AM
Fix to line 56:

$file_name = $person.time().$_FILES['userfile']['name'];

Want to hide these adverts? Register an account for free!