86Stang Posted December 1, 2009 Share Posted December 1, 2009 I've got this bit of code: if ($file != "") { $goodfiles = array("jpg","pdf","doc","zip","xls","txt"); $ext = explode(".", $_FILES['file']['name']); // create a filename for the uploaded image $filename = date("Ymd-his") . "." . $ext[1]; $file_url = "repository/" . $filename; // file_url to store in db for ($i=0;$i<count($goodfiles);$i++) // dont allow file types outside list { if ($ext[1] != $goodfiles[$i]) { $msg = "This file type is not allowed."; } else { unset($msg); break; } } // copy the pic to the current directory and delete the temporary file if (!copy($_FILES['file']['tmp_name'], $filepath . $filename)) { $msg = "There was an error uploading your file.<br>"; } unlink($_FILES['file']['tmp_name']); // update file in db $qry = "UPDATE table SET title='$file_title',url='$file_url' WHERE event_id = $event_id"; mysql_query($qry) or die ("Error during query!"); } It seems like out of nowhere the extensions are not uploading as part of $file_url. In other words, I upload a file that should look like 20091201-103437.pdf and it ends up looking like 20091201-103437. (<-- dot included). On top of that, I get the following error, even though the file is uploading (minus the extension): Warning: unlink(): No such file or directory in /path/to/file.php on line 77 Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/ Share on other sites More sharing options...
newbtophp Posted December 1, 2009 Share Posted December 1, 2009 if ($file != "") { $filename = $_FILES['file']['name']; $goodfiles = array("jpg","pdf","doc","zip","xls","txt"); $ext = strtolower(end(explode('.',$filename))); if(in_array($ext,$goodfiles)) { // copy the pic to the current directory and delete the temporary file if (!copy($_FILES['file']['tmp_name'], $filepath . $filename)){ $msg = "There was an error uploading your file.<br>"; } unlink($_FILES['file']['tmp_name']); // update file in db $qry = "UPDATE table SET title='$file_title',url='$file_url' WHERE event_id = $event_id"; mysql_query($qry) or die ("Error during query!"); } else { $msg = "This file type is not allowed."; } } Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969151 Share on other sites More sharing options...
86Stang Posted December 1, 2009 Author Share Posted December 1, 2009 That's kicking out "This file type is not allowed" when I tried uploading a txt, pdf or jpg. Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969155 Share on other sites More sharing options...
mrMarcus Posted December 1, 2009 Share Posted December 1, 2009 if you echo out $_FILES['file']['name'], what do you get? Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969183 Share on other sites More sharing options...
86Stang Posted December 1, 2009 Author Share Posted December 1, 2009 Not quite sure where I should echo it out but I appended it to the $msg line: $msg = "This file type is not allowed." . $_FILES['file']['name']; and it's not showing anything other than the "This file type is not allowed" string. Should I put it somewhere else? Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969245 Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 I suggest your turn error reporting on error_reporting(E_ALL); ini_set("display_errors", 1); so that PHP will tell you when you have undefined indexes. It seems like the $_FILES array doesn't have what you expect it to. Try adding print_r($_FILES); in your code to see what the structure of the FILES array is Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969249 Share on other sites More sharing options...
86Stang Posted December 1, 2009 Author Share Posted December 1, 2009 Thanks for the help so far! I got a return of: Array() on the print_r and Undefined index: file in /path/to/file.php on line 57 and line 57 is: $filename = $_FILES['file']['name']; Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969338 Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 Ok, that means that your $_FILES array is empty. Are you sure you are submitting it correctly? can I see the form? you need to set the enctype of your form to "multipart/form-data" when uploading files Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969342 Share on other sites More sharing options...
86Stang Posted December 1, 2009 Author Share Posted December 1, 2009 Here's the quick and dirty version of it (stripped out all the styling and non-related data: <form action="?opt=edit_event&action=edit_event_exec" method="post"> <input type="hidden" name="required" value="time,location,city,state,zip,info,title"> <input type="hidden" name="event_id" value="<? echo $row[event_id]; ?>"> <input type="hidden" name="page" value="<? echo $page; ?>"> <input type="text" name="file_title" value="<? echo $row[file_title]; ?>"> <input type="file" name="file"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969355 Share on other sites More sharing options...
rajivgonsalves Posted December 1, 2009 Share Posted December 1, 2009 as Mike said your missing the enctype add it in <form action="?opt=edit_event&action=edit_event_exec" method="post" enctype="multipart/form-data"> Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969366 Share on other sites More sharing options...
86Stang Posted December 1, 2009 Author Share Posted December 1, 2009 What the ..... GRRR!!! I KNOW I checked for that early on. I wonder if I was looking at the wrong tab of code. None the less, it worked. Thanks so much for the help and sorry for being an idiot! Quote Link to comment https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/#findComment-969372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.