rvdb86 Posted February 14, 2009 Share Posted February 14, 2009 hi, i am trying to upload files through php but my script is not working. i would really appreciate it if some one could tell me why the following script isn't working ??? <?php // Declare variables // Get the basic file information $userfile = $_FILES['userfile']['name']; $file_size = $_FILES['userfile']['size']; $file_temp = $_FILES['userfile']['tmp_name']; $file_err = $_FILES['userfile']['error']; $path = '../../sites/'.$_POST['site'].'/files'; // Create a new file name // This is so if other files on the server have the same name, it will be renamed // $randomizer = rand(0000, 9999); // $file_name = $randomizer.$userfile; // Get the file type // Using $_FILES to get the file type is sometimes inaccurate, so we are going // to get the extension ourselves from the name of the file // This also eliminates having to worry about the MIME type $file_type = $userfile; $file_type_length = strlen($file_type) - 3; $file_type = substr($file_type, $file_type_length); if(!empty($userfile)) { // limit the size of the file to 200KB if($file_size > 25600) { echo 'FILE SIZE TO LARGE<BR />'; exit(); } // Set allowed file types // Set case of all letters to lower case $file_type = strtolower($file_type); $files = array(); $files[] = 'doc'; $files[] = 'mdb'; $files[] = 'gif'; $files[] = 'png'; $files[] = 'ods'; $files[] = 'pdf'; $files[] = 'rtf'; $files[] = 'xls'; $files[] = 'docx'; $files[] = 'odp'; $files[] = 'ods'; $files[] = 'odt'; $files[] = 'pptx'; $files[] = 'ps'; $files[] = 'pub'; $files[] = 'xlsx'; $files[] = 'pcx'; $files[] = 'bmp'; $files[] = 'gif'; $files[] = 'jpg'; $files[] = 'jpeg'; $files[] = 'png'; $files[] = 'tga'; //$files[] = 'tiff'; $files[] = 'aac'; $files[] = 'ac3'; $files[] = 'flac'; $files[] = 'mpa'; $files[] = 'mp3'; $files[] = 'ra'; $files[] = 'ram'; $files[] = 'wav'; $files[] = 'wma'; $files[] = 'ogg'; $files[] = 'mp4'; $files[] = '3g2'; $files[] = '3gp'; $files[] = 'avi'; $files[] = 'flv'; $files[] = 'gvi'; $files[] = 'm4v'; $files[] = 'mov'; $files[] = 'mpg'; $files[] = 'rmvb'; $files[] = 'vod'; $files[] = 'wmv'; //$files[] = 'ipod'; $files[] = '7z'; $files[] = 'tar'; $files[] = 'rar'; $files[] = 'yz1'; $files[] = 'zip'; // Search the array for the allowed file type $key = array_search($file_type, $files); if($key) { } else { echo '<b>ILLEGAL FILE TYPE</b><br />'; exit(); } // Check for errors and upload the file $error_count = count($file_error); if($error_count > 0) { for($i = 0; $i <= $error_count; ++$i) { echo $_FILES['userfile']['error'][$i]; } } else { if(move_uploaded_file($_FILES['userfile']['tmp_name'], "$path/$file_name")){ //if(move_uploaded_file($file_temp, "$path/$file_name")) { header("location: ../files"); } else { echo '<h3>ERROR</h3>'; } } } else { echo '<h3>No file has been selected.</h3>'; } ?> it keeps returning the error message TIA! Link to comment https://forums.phpfreaks.com/topic/145239-uploading-files/ Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 what error does it return? Scott. Link to comment https://forums.phpfreaks.com/topic/145239-uploading-files/#findComment-762417 Share on other sites More sharing options...
rvdb86 Posted February 14, 2009 Author Share Posted February 14, 2009 it echos the error messsage from this part of the scrtip (near the end) ... } else { if(move_uploaded_file($_FILES['userfile']['tmp_name'], "$path/$file_name")){ //if(move_uploaded_file($file_temp, "$path/$file_name")) { header("location: ../files"); } else { echo '<h3>ERROR</h3>'; } ... Link to comment https://forums.phpfreaks.com/topic/145239-uploading-files/#findComment-762419 Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 what are the permissions on the destination directory? they need to be writable by php Scott. Link to comment https://forums.phpfreaks.com/topic/145239-uploading-files/#findComment-762424 Share on other sites More sharing options...
printf Posted February 14, 2009 Share Posted February 14, 2009 // Declare variables That's funny... Why do you copy variables? I don't understand that. Your array_search() is looking for extensions of only (3) characters, that won't work for some of the files in your $files array. That could cause an error! Also, PHP gives you CORE functions to check the upload file, but you don't use any of them. Trust me the CORE way is provided to make the processes safer, learn it! Link to comment https://forums.phpfreaks.com/topic/145239-uploading-files/#findComment-762425 Share on other sites More sharing options...
rvdb86 Posted February 14, 2009 Author Share Posted February 14, 2009 the permissions are 777 i read in a tutorial that the file type function to check the files are not 100% reliable. was what i read wrong? Link to comment https://forums.phpfreaks.com/topic/145239-uploading-files/#findComment-762429 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.