html - PHP: File upload not working properly -
hi i'm new php(i'm asp.net developer) , switched php because needed develop website friend. trying upload music files folder on website keeps on getting error:
notice: "undefined index: uploadedfile in c:\xampp\htdocs\sitename\admin\uploader.php on line 5"
i've tried following steps:
- set max_upload_filesize , post_max_size "1028m".
- added "enctype="multipart/form-data" form tag.
- tried "print_r($_files);" , array keeps on returning empty.
- used upload script in new website , works fine.
- tried many different scripts online none worked , tried still facing same problem.
here code below:
html
<!doctype html> <html > <head> <title>controlhome</title> <meta charset="utf-8"> <link rel="stylesheet" href="../css/style.css"> <link rel="stylesheet" href="css/admin.css"> <script src="../js/jquery-1.7.1.min.js"></script> <script src="../js/superfish.js"></script> <script src="../js/jquery.easing.1.3.js"></script> <script src="../js/tms-0.4.1.js"></script> <script src="../js/slider.js"></script> <!--[if lt ie 8]> <div style=' clear: both; text-align:center; position: relative;'> <a href="http://windows.microsoft.com/en-us/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode"> <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="you using outdated browser. faster, safer browsing experience, upgrade free today." /> </a> </div> <![endif]--> <!--[if lt ie 9]> <script src="../js/html5.js"></script> <link rel="stylesheet" href="css/ie.css"> <![endif]--> </head> <body> <div class="main-bg"> <div class="page"> <form method="get" action="uploader.php" enctype="multipart/form-data> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <form enctype="multipart/form-data" action="uploader.php" method="post"> <!-- max_file_size must precede file input field --> choose file upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="upload file" /> </td> <td> </td> <td> </td> <td> </td> </tr> </table> </form> </div> </div> </div> </div> </section> <!-- footer --> <footer> </footer> </div> </body> </html>
php
<?php ini_set('upload_max_filesize', '1028m'); ini_set('post_max_size', '1028m'); echo $_files['uploadedfile']['error']; print_r($_files); //********************************************************************************************** echo "please wait while attempt upload file...<br><br>"; //********************************************************************************************** $target_path = "uploads/"; $flag = 0; // safety net, if gets 1 @ point in process, don't upload. $filename = $_files['uploadedfile']['name']; $filesize = $_files['uploadedfile']['size']; $mimetype = $_files['uploadedfile']['type']; $filename = htmlentities($filename); $filesize = htmlentities($filesize); $mimetype = htmlentities($mimetype); $target_path = $target_path . basename( $filename ); if($filename != ""){ echo "beginning upload process file named: ".$filename."<br>"; echo "filesize: ".$filesize."<br>"; echo "type: ".$mimetype."<br><br>"; } //first generate md5 hash of new file name //force mp3 extention on file uploading $hashedfilename = md5($filename); $hashedfilename = $hashedfilename.".mp3"; //check empty file if($filename == ""){ $error = "no file exists!"; $flag = $flag + 1; } //now check file doesn't exist. $existname = "uploads/".$hashedfilename; if(file_exists($existname)){ if($flag == 0){ $error = "your file exists on server! please choose file upload or rename file on computer , try uploading again!"; } $flag = $flag + 1; } //whitelisted files - allow files mp3 extention onto server... $whitelist = array(".mp3"); foreach ($whitelist $ending) { if(substr($filename, -(strlen($ending))) != $ending) { $error = "the file type or extention trying upload not allowed! can upload mp3 files server!"; $flag++; } } //now check filesize. if big or small reject //mp3 files should @ least 1mb , no more 6.5 mb if($filesize > 6920600){ //file large if($flag == 0){ $error = "the file trying upload large! file can 6.5 mb in size only. please upload smaller mp3 file or encode file lower bitrate."; } $flag = $flag + 1; } if($filesize < 1048600){ //file small if($flag == 0){ $error = "the file trying upload small! file has been marked suspicious because our system has determined small valid mp3 file. valid mp3 files must bigger 1 mb , smaller 6.5 mb."; } $flag = $flag + 1; } //check mimetype of file if($mimetype != "audio/x-mp3" , $mimetype != "audio/mpeg"){ if($flag == 0){ $error = "the file trying upload not contain expected data. sure file mp3?"; } $flag = $flag + 1; } //check file mp3 file reading first few characters of file $f = @fopen($_files['uploadedfile']['tmp_name'],'r'); $s = @fread($f,3); @fclose($f); if($s != "id3"){ if($flag == 0){ $error = "the file attempting upload not appear valid mp3 file."; } $flag++; } //all checks done, move file... if($flag == 0){ if(move_uploaded_file($_files['uploadedfile']['tmp_name'], $target_path)) { //change filename md5 hash , force mp3 extention. if(@file_exists("uploads/".$filename)){ //rename file md5 version rename("uploads/".$filename, "uploads/".$hashedfilename); echo "the file ". basename( $filename ). " has been uploaded. file <a href='uploads/$hashedfilename'>here</a>."; } else{ echo "there error uploading file, please try again!"; } } else{ echo "there error uploading file, please try again!"; } } else { echo "file upload failed!<br>"; if($error != ""){ echo $error; } } ?>
here try this
<!doctype html> <html > <head> <title>controlhome</title> <meta charset="utf-8"> <link rel="stylesheet" href="../css/style.css"> <link rel="stylesheet" href="css/admin.css"> <script src="../js/jquery-1.7.1.min.js"></script> <script src="../js/superfish.js"></script> <script src="../js/jquery.easing.1.3.js"></script> <script src="../js/tms-0.4.1.js"></script> <script src="../js/slider.js"></script> <!--[if lt ie 8]> <div style=' clear: both; text-align:center; position: relative;'> <a href="http://windows.microsoft.com/en-us/internet-explorer/products/ie/home?ocid=ie6_countdown_bannercode"> <img src="http://storage.ie6countdown.com/assets/100/images/banners/warning_bar_0000_us.jpg" border="0" height="42" width="820" alt="you using outdated browser. faster, safer browsing experience, upgrade free today." /> </a> </div> <![endif]--> <!--[if lt ie 9]> <script src="../js/html5.js"></script> <link rel="stylesheet" href="css/ie.css"> <![endif]--> </head> <body> <div class="main-bg"> <div class="page"> <form enctype="multipart/form-data" action="uploader.php" method="post"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> <!-- max_file_size must precede file input field --> choose file upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="upload file" /> </td> </tr> </table> </form> </div> </div> </body> </html>
you need remove form tag , use post method.
Comments
Post a Comment