alxsmth Posted March 3, 2007 Share Posted March 3, 2007 Hey, I have been trying to get a php upload script to work which uploads an mp3 file to the server and then updates an xml playlist for a flash mp3 player. It seemed to work once, but majority of the time i get the same error - "change permission to 777 failed". I am unable to change file permissions on my server but the fact that it worked once makes me think that this may not be the problem. Could anyone explain to me why this might be or suggest an alternate script i might use? Many thanks here is the script..... <?php //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv // You may change maxsize, and allowable upload file types. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //Maximum file size. You may increase or decrease. $MAX_SIZE = 10000000; //Allowable file ext. names. you may add more extension names. $FILE_EXTS = array('.jpg','.gif','.bmp','.jpeg','.mp3'); //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 = "files/"; //Image file types checked for in the writting the .xml $imgfilecheck = array(".jpg",".gif",".bmp",".jpeg"); //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ /************************************************************ * 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."/files/"; $message =""; /************************************************************ * Create Upload Directory ************************************************************/ if (!is_dir("files")) { if (!mkdir($upload_dir)) die ("upload_files 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 10MB."; //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 <annotation>".str_replace(".mp3","",$file)."</annotation>\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; } ?> Please use the code ( ) tags when posting code in posts. Thank you. - wildteen88 Link to comment https://forums.phpfreaks.com/topic/41003-mp3-upload/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.