...

View Full Version : 2 "browse" boxes/1 "submit"



wayne3503
08-14-2007, 06: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
?>

Fumigator
08-14-2007, 08:54 PM
The PHP manual contains a good chapter on uploading multiple files.

http://us2.php.net/manual/en/features.file-upload.multiple.php

wayne3503
08-14-2007, 10:02 PM
Considering how much code is already here, would it be difficult to integrate arrays if im not already using them?

Fumigator
08-14-2007, 10:13 PM
Would it be difficult? Hmm... I dunno, would it? Yes you'll have to modify the code... doesn't strike me as difficult really, just maybe tedious. Make a backup so when-- err, if you destroy the file you can get back to where you were.

wayne3503
08-14-2007, 10:34 PM
No, you were right the first time when you said "when"..lol One thing i forgot to mention and just now hit me as I was just playing with the code, is that it is made for mp3 and flv, so how would I go about adding support for jpg and gif in this script?
Thanks again

Fumigator
08-14-2007, 10:41 PM
Add more extensions to the $FILE_EXTS array.

wayne3503
08-14-2007, 10:55 PM
oh ok...now I feel like a moron. i knew I had to do that, just wasnt sure if thats all I had to do.. :D
thanks

wayne3503
08-15-2007, 04:27 PM
Well I know I need to add another
<input type="file" id="userfile" name="userfile"> but do i leave it all the same name and id and all that? I would assume not. And since ill need to change that stuff what other changes do I need to make to account for it.
Thanks for the help

wayne3503
08-15-2007, 08:35 PM
Any ideas on this? Maybe im a little confused.

Fumigator
08-15-2007, 09:38 PM
Did you read the page in the manual I linked to?

wayne3503
08-15-2007, 09:58 PM
Yes i did. im just not sre what all i need to do..

wayne3503
08-24-2007, 09:05 PM
Ok so now I have my 2 browse boxes and 1 submit button. Now its all working great and just need to do a little tweak but I have tried a few things with no success. Here is my code right now...

<?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');

//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');
$FILE_EXTS_IMG = array('.jpg', '.jpeg', '.png', '.gif');

//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 hcking
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['userfile1'])&&($_FILES['userfile2'])) {
$resource = fopen("log.txt","a");
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]".$_FILES['userfile1']['name']." ".$_FILES['userfile1']['type']."\n");
fclose($resource);
fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]".$_FILES['userfile2']['name']." ".$_FILES['userfile2']['type']."\n");
fclose($resource);
$file_type1 = $_FILES['userfile1']['type'];
$file_name1 = $_FILES['userfile1']['name'];
$file_type2 = $_FILES['userfile2']['type'];
$file_name2 = $_FILES['userfile2']['name'];
$file_ext1 = strtolower(substr($file_name1,strrpos($file_name1,".")));
$file_ext2 = strtolower(substr($file_name2,strrpos($file_name2,".")));

//File Size Check
if (($_FILES['userfile1']['size'] > $MAX_SIZE)||($_FILES['userfile2']['size'] > $MAX_SIZE)) $message = "The file size is over 20MB.";

//File Extension Check
else if (!in_array($file_ext1, $FILE_EXTS)) $message = "Sorry, $file_name1($file_type1) is not allowed to be uploaded.";
else if (!in_array($file_ext2, $FILE_EXTS_IMG)) $message = "Sorry, $file_name2($file_type2) 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 $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<image>$url_dir/$upload_dir".str_replace(".mp3",".jpg",$file)."</image>\n";

if ($info == "yes") { $stringdata .= " <info>$url_dir/$upload_dir$file</info>\n"; }
if (file_exists($_FILES['userfile2']['tmp_name'])) { $stringdata .= " <img>$url_dir/$upload_dir".$file_name2."</img>\n"; }
$stringdata .= "</track>\n";
}
}
$stringdata .= "\n\n </trackList>\n</playlist>";
$fh = fopen($savefile, 'w');
fwrite($fh, $stringdata);
fclose($fh);
function do_upload($upload_dir, $upload_url) {
$temp_name1 = $_FILES['userfile1']['tmp_name'];
$file_name1 = $_FILES['userfile1']['name'];
$file_name1 = str_replace("\\","",$file_name1);
$file_name1 = str_replace("'","",$file_name1);
$file_path1 = $upload_dir.$file_name1;
$temp_name2 = $_FILES['userfile2']['tmp_name'];
$file_name2 = $_FILES['userfile2']['name'];
$file_name2 = str_replace("\\","",$file_name2);
$file_name2 = str_replace("'","",$file_name2);
$file_path2 = $upload_dir.$file_name2;

//File Name Check
if (($file_name1 =="")||($file_name2 =="")) {
$message = "Invalid File Name Specified";
return $message;
}
$result1 = move_uploaded_file($temp_name1, $file_path1);
$result2 = move_uploaded_file($temp_name2, $file_path2);
if (!chmod($file_path1,0777)) $message = "change permission to 777 failed.";
else $message = ($result1 && $result2) ? "$file_name1 &amp; $file_name2 uploaded successfully." : "Somthing is wrong with uploading a file.";
return $message;
}
?>
<html>
<head>

</head>
<body>
<center>
<font color=red><?=$_REQUEST[message]?></font>
<br>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
Upload File 1 <input type="file" id="userfile1" name="userfile1"><br />
Upload File 2 <input type="file" id="userfile2" name="userfile2"><br />
<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
?>
Which will give me this for my xml file...

<?xml version="1.0" encoding="UTF-8" ?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>

<track>
<title>01 Wake</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/01 Wake.mp3</location>
<img></img> </track>
<track>
<title>04 Bleed It Out</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/04 Bleed It Out.mp3</location>
<img></img> </track>
<track>
<title>07 Hands Held High</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/07 Hands Held High.mp3</location>
<img></img> </track>
<track>
<title>08 No More Sorrow</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/08 No More Sorrow.mp3</location>
<img></img> </track>

<track>
<title>ptsofathrty.flv</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/ptsofathrty.flv</location>
<img></img> </track>

</trackList>

</playlist>
What I need to do for my xml is get this kind of output...

<?xml version="1.0" encoding="UTF-8" ?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>

<track>
<title>01 Wake</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/01 Wake.mp3</location>
<img>http://localhost/viewable/hsrevised/live/cms/music/some_image.jpg</img> </track>
<track>
<title>04 Bleed It Out</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/04 Bleed It Out.mp3</location>
<img>http://localhost/viewable/hsrevised/live/cms/music/another_image.jpg</img> </track>
<track>
<title>07 Hands Held High</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/07 Hands Held High.mp3</location>
<img>http://localhost/viewable/hsrevised/live/cms/music/a_different_image.jpg</img> </track>
<track>
<title>08 No More Sorrow</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/08 No More Sorrow.mp3</location>
<img>http://localhost/viewable/hsrevised/live/cms/music/yet_another_image.jpg</img> </track>

<track>
<title>ptsofathrty.flv</title>
<location>http://localhost/viewable/hsrevised/live/cms/music/ptsofathrty.flv</location>
<img>http://localhost/viewable/hsrevised/live/cms/music/maybe_an_image_here.jpg</img> </track>

</trackList>

</playlist>
Any ideas how i would achieve this?

Thanks alot for all the help

Fumigator
08-24-2007, 10:35 PM
if (file_exists($_FILES['userfile2']['tmp_name'])) {


This condition statement occurs after you have run move_uploaded_file() which deletes the temp file so it will never be true (unless the move failed).

wayne3503
08-24-2007, 11:07 PM
What should be done with that line then, just delete it?

Fumigator
08-25-2007, 12:07 AM
I would probably change it to check to see if the permanent file exists (rather than the temp file).

wayne3503
08-25-2007, 12:39 AM
So in my case I would use...

if (file_exists($_FILES['userfile2']['name'])) {
correct?

wayne3503
08-26-2007, 12:31 AM
so any other ideas on how to accomplish what I am trying to do?
Thanks

wayne3503
08-27-2007, 04:32 PM
Any ideas at all on this one?

Fumigator
08-27-2007, 06:49 PM
From your code:


.
.
.
$result2 = move_uploaded_file($temp_name2, $file_path2);
.
.
.


That variable $file_path2 is where you moved the file. So figure out the value of that variable and then check to see if the file exists and if it does then echo out your xml string for it.

wayne3503
08-28-2007, 06:09 PM
so if i do that then i can still have the 2 files be in the same path correct?

wayne3503
08-30-2007, 05:23 PM
sorry, guess im just a little lost.

wayne3503
08-31-2007, 04:59 PM
Im still not sure what all needs to be done for this. Any further help would be greatly appreciated

Fumigator
08-31-2007, 09:22 PM
What is it that doesn't work?

wayne3503
08-31-2007, 09:33 PM
well I want to make the two boxes format specific. By this I mean that ONLY mp3 and flv files can be loaded into one and ONLY images can be loaded into the other. Im just not sure I understand the code you have. I mean because when I look through my code im assuming I would have to change ALOT but not sure of all I need to do to accomplish this. But if I cant get it to work then I will just leave it as is.

Thanks for your help

Fumigator
09-01-2007, 06:34 AM
Seems to me you've already got this going with this:


$FILE_EXTS = array('.mp3', '.flv');
$FILE_EXTS_IMG = array('.jpg', '.jpeg', '.png', '.gif');

wayne3503
09-04-2007, 05:59 AM
Well that may be started there but as you can see my browse boxes are named userfile1 and userfile2. I guess where im lost is how to strictly apply the formats to each of the boxes.

Thanks for the help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum