Jump to content

Uploaded file not retaining extension


86Stang

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/183616-uploaded-file-not-retaining-extension/
Share on other sites

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.";

}

   }

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?

 

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

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.