wayne3503
08-14-2007, 05:27 PM
Ok so I have this upload script and everything is working great. I have it set up so that it will upload an mp3 to a desired directory and then add an entry for the song in a playlist.xml file. Now I need to turn it up a notch. I dont know if this is possible but i guess ill never know unless i ask, so here it goes. I need to know if in addition to my mp3 upload box I can have an upload box for an image (album cover) but have just one submit button? This way a user can submit a song and an album image, the song will get added to the xml and listed in the player I have and the image will also get added to the xml and displayed next to song title in the player. Anyway all I realy need to know is how to have these two "browse" boxes and one upload. The rest im sure I can figure out.
Thankx for the help and here is the code
<?php
//This page allows users to upload files tot he server
//Set the page title and include the header
$page_title = 'Upload a Song';
include ('includes/inc.header.php');
//include_once ('includes/class.id3.php');
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// You may change maxsize, and allowable upload file types.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Maximum file size. You may increase or decrease.
$MAX_SIZE = 25000000;
//Allowable file ext. names. you may add more extension names. ONLY MP3 AND FLV AUDIO.
$FILE_EXTS = array('.mp3', '.flv');
//Allow file delete? no, if only allow upload only.
$DELETABLE = true;
/************************************************************
* Setup variables
************************************************************/
//File you wish to save the playlist to.
$savefile = "playlist.xml";
//Allow download of MP3s.
$info = "no";
//Directory uploaded files go to.
$upload_dir = "music/";
//Image file types checked for in the writting the .xml
//$imgfilecheck = array(".jpg",".gif",".bmp",".jpeg");
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Do not touch the below if you are not confident.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/************************************************************
* Other variables
************************************************************/
$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_url = $url_dir."/music/";
$message ="";
/************************************************************
* Create Upload Directory
************************************************************/
if (!is_dir("music")) {
if (!mkdir($upload_dir))
die ("upload_music directory doesn't exist and creation failed");
if (!chmod($upload_dir,0777))
die ("change permission to 777 failed.");
}
/************************************************************
* Process User's Request
************************************************************/
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
fclose($resource);
if (strpos($_REQUEST[del],"/.")>0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 20MB.";
//File Extension Check
else if (!in_array($file_ext, $FILE_EXTS))
$message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
else
$message = do_upload($upload_dir, $upload_url);
print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";
/************************************************************
* List Files/Update XML
************************************************************/
$handle=opendir($upload_dir);
$filelist = "";
$stringdata .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
$stringdata .= "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n <trackList>\n";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<tr><td><sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub></td>
<td><a href='$upload_dir$file'>".$file."</a>";
if (!$DELETABLE)
$filelist .= "</td><td>Not Supported</td></tr>";
if ($DELETABLE)
$filelist .= "</td><td><center><a href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a></center></td></tr>";
$stringdata .= "\n <track>\n <title>".str_replace(".mp3","",$file)."</title>\n <location>$url_dir/$upload_dir$file</location>\n";
if ($info == "yes")
{$stringdata .= " <info>$url_dir$upload_dir$file</info>\n";}
if (file_exists(str_replace(".mp3",$ingfilecheck,$file)))
{$stringdata .= " <img>$url_dir$upload_dir".str_replace(".mp3",".jpg",$file)."</img>\n";}
$stringdata .= " </track>";
}
}
$stringdata .= "\n\n </trackList>\n
</playlist>";
$fh = fopen($savefile, 'w');
fwrite($fh, $stringdata);
fclose($fh);
function do_upload($upload_dir, $upload_url) {
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_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 =="") {
$message = "Invalid File Name Specified";
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
{$message = ($result)?"$file_name uploaded successfully." :
"Somthing is wrong with uploading a file.";}
return $message;
}
?>
<html>
<head>
<font color=red><?=$_REQUEST[message]?></font>
</head>
<body>
<center>
<br>
<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>
<br><b>My Music</b>
<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="15%">Upload Date:</td>
<td align="left" width="70%">File:</td>
<td align="center" width="10%">Delete?</td>
</tr>
<?=$filelist?>
</table>
<small><sup>Developed By:
<a style="text-decoration:none">Crazy8 Designs</a><br>
</sup></small>
</center>
</body>
</html>
<?php
include ('footer.php'); //include footer
?>
Thankx for the help and here is the code
<?php
//This page allows users to upload files tot he server
//Set the page title and include the header
$page_title = 'Upload a Song';
include ('includes/inc.header.php');
//include_once ('includes/class.id3.php');
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// You may change maxsize, and allowable upload file types.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Maximum file size. You may increase or decrease.
$MAX_SIZE = 25000000;
//Allowable file ext. names. you may add more extension names. ONLY MP3 AND FLV AUDIO.
$FILE_EXTS = array('.mp3', '.flv');
//Allow file delete? no, if only allow upload only.
$DELETABLE = true;
/************************************************************
* Setup variables
************************************************************/
//File you wish to save the playlist to.
$savefile = "playlist.xml";
//Allow download of MP3s.
$info = "no";
//Directory uploaded files go to.
$upload_dir = "music/";
//Image file types checked for in the writting the .xml
//$imgfilecheck = array(".jpg",".gif",".bmp",".jpeg");
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
// Do not touch the below if you are not confident.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/************************************************************
* Other variables
************************************************************/
$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_url = $url_dir."/music/";
$message ="";
/************************************************************
* Create Upload Directory
************************************************************/
if (!is_dir("music")) {
if (!mkdir($upload_dir))
die ("upload_music directory doesn't exist and creation failed");
if (!chmod($upload_dir,0777))
die ("change permission to 777 failed.");
}
/************************************************************
* Process User's Request
************************************************************/
if ($_REQUEST[del] && $DELETABLE) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
fclose($resource);
if (strpos($_REQUEST[del],"/.")>0); //possible hacking
else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
else if (substr($_REQUEST[del],0,6)==$upload_dir) {
unlink($_REQUEST[del]);
print "<script>window.location.href='$url_this?message=deleted successfully'</script>";}
}
else if ($_FILES['userfile']) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
.$_FILES['userfile']['name']." "
.$_FILES['userfile']['type']."\n");
fclose($resource);
$file_type = $_FILES['userfile']['type'];
$file_name = $_FILES['userfile']['name'];
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
//File Size Check
if ( $_FILES['userfile']['size'] > $MAX_SIZE)
$message = "The file size is over 20MB.";
//File Extension Check
else if (!in_array($file_ext, $FILE_EXTS))
$message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
else
$message = do_upload($upload_dir, $upload_url);
print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else
$message = "Invalid File Specified.";
/************************************************************
* List Files/Update XML
************************************************************/
$handle=opendir($upload_dir);
$filelist = "";
$stringdata .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
$stringdata .= "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">\n <trackList>\n";
while ($file = readdir($handle)) {
if(!is_dir($file) && !is_link($file)) {
$filelist .= "<tr><td><sub><small><small><font color=grey> ".date("d-m H:i", filemtime($upload_dir.$file))
."</font></small></small></sub></td>
<td><a href='$upload_dir$file'>".$file."</a>";
if (!$DELETABLE)
$filelist .= "</td><td>Not Supported</td></tr>";
if ($DELETABLE)
$filelist .= "</td><td><center><a href='?del=$upload_dir".urlencode($file)."' title='delete'>x</a></center></td></tr>";
$stringdata .= "\n <track>\n <title>".str_replace(".mp3","",$file)."</title>\n <location>$url_dir/$upload_dir$file</location>\n";
if ($info == "yes")
{$stringdata .= " <info>$url_dir$upload_dir$file</info>\n";}
if (file_exists(str_replace(".mp3",$ingfilecheck,$file)))
{$stringdata .= " <img>$url_dir$upload_dir".str_replace(".mp3",".jpg",$file)."</img>\n";}
$stringdata .= " </track>";
}
}
$stringdata .= "\n\n </trackList>\n
</playlist>";
$fh = fopen($savefile, 'w');
fwrite($fh, $stringdata);
fclose($fh);
function do_upload($upload_dir, $upload_url) {
$temp_name = $_FILES['userfile']['tmp_name'];
$file_name = $_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 =="") {
$message = "Invalid File Name Specified";
return $message;
}
$result = move_uploaded_file($temp_name, $file_path);
if (!chmod($file_path,0777))
$message = "change permission to 777 failed.";
else
{$message = ($result)?"$file_name uploaded successfully." :
"Somthing is wrong with uploading a file.";}
return $message;
}
?>
<html>
<head>
<font color=red><?=$_REQUEST[message]?></font>
</head>
<body>
<center>
<br>
<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>
<br><b>My Music</b>
<table border="0" width="100%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="15%">Upload Date:</td>
<td align="left" width="70%">File:</td>
<td align="center" width="10%">Delete?</td>
</tr>
<?=$filelist?>
</table>
<small><sup>Developed By:
<a style="text-decoration:none">Crazy8 Designs</a><br>
</sup></small>
</center>
</body>
</html>
<?php
include ('footer.php'); //include footer
?>