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
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
Use an uploader?
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.
Use that, change it round if you wish.PHP Code:
<?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='[IMG]www.".$sitename.".com/files/upload/".$img."[/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
}
?>
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
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 Code:
<?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?>
Last edited by NN-Dom; 02-08-2008 at 01:04 PM.
Erm..
I get this error with the above method.
Code:Parse error: syntax error, unexpected '.' in /home/chrism/public_html/habbohut/apps/trialup.php on line 56
Replace the . on line 56 with a ,
hah my mistake, sorry - here you go
PHP Code:
<?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.hah my mistake, sorry - here you go
PHP Code:
<?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?>
Want to hide these adverts? Register an account for free!