Page 1 of 1

changing file upload name

Posted: Mon Oct 19, 2009 1:38 pm
by phpnoobyyy
Hi guys

I have a bit of code that changes the name of a file upload. The code executes perfectly on firefox but for some reason fails to execute on IE, Safari, Opera. I have failed to find a reason why it fails to execute on these browsers. Any help would be greatly appreciated.

the code is as follows:

Code: Select all

// Validate the type. Should be msword.
$allowed = array('application/msword');
if (in_array($_FILES['upload']['type'], $allowed)) {
        
function isAllowed($fileName) {
global $allowed;
 
return in_array(end(explode(".", $fileName)), $allowed);
}       
        $target = "../uploads";
$target = $target . basename( $_FILES['upload']['name']) ;
$ok=1;
 
$explode = explode('.', $_FILES['uploaded']['name']);
$extension = array_pop($explode);
$newname = rand(0, pow(10, 5)).date("M d, Y").$_POST['email'].$extension;
$basename = date("d.m.Y").$_POST['email'].$extension.( $_FILES['upload']['name']);
$base_file = ( $_FILES['upload']['name']);
        
// Move the file over.
if (move_uploaded_file ($_FILES['upload']['tmp_name'], "../uploads/$newname.{$_FILES['upload']['name']}")) {
echo '<p><em>The file has been uploaded!</em></p>';
} // End of move... IF.
}

Thanks for taking the time out to help.

phpnoobyyy

Re: changing file upload name

Posted: Mon Oct 19, 2009 1:46 pm
by requinix
IE is notorious for not sending the right MIME type with file uploads. That means the

Code: Select all

$allowed = array('application/msword');
if (in_array($_FILES['upload']['type'], $allowed)) {
part of your code probably won't work.

You can check the file type yourself (it's easy) or just look at the file extension.

Re: changing file upload name

Posted: Mon Oct 19, 2009 2:58 pm
by phpnoobyyy
Thanks for responding.

Have just checked the mime types again, and nothing appears to be out of place. To check if it was the mime type, i changed it to allow upload of jpg instead of msword, and again it failed to execute in the browsers listed.

Perhaps the mime file type is not the issue....?

Re: changing file upload name

Posted: Mon Oct 19, 2009 4:18 pm
by requinix
Then you'll need to explain what
phpnoobyyy wrote:but for some reason fails to execute on IE, Safari, Opera
means.

Re: changing file upload name

Posted: Mon Oct 19, 2009 6:01 pm
by phpnoobyyy
Ok, i seem to have narrowed the problem down a little further. I believe the problem may lie in this bit of coding from the original posted code:

Code: Select all

$explode = explode('.', $_FILES['uploaded']['name']);
$extension = array_pop($explode);
$newname = rand(0, pow(10, 5)).date("M d, Y").$_POST['email'].$extension;
$basename = date("d.m.Y").$_POST['email'].$extension.( $_FILES['upload']['name']);
$base_file = ( $_FILES['upload']['name']);
Having taken this element of the code out of the script, it processes in all browsers. However, this coding is a necessary part of the script as it renames the uploaded file primarily to avoid file overwrite problems where users inadvertently upload files with the same name.

I'd be grateful for any further input to try to solve this.

Thanks

phpnoobyyy

Re: changing file upload name

Posted: Tue Oct 20, 2009 9:53 am
by McInfo

Code: Select all

$_FILES['uploaded']['name']
$_FILES['upload']['name']
Edit: This post was recovered from search engine cache.

Re: changing file upload name

Posted: Tue Oct 20, 2009 2:47 pm
by phpnoobyyy
No joy...I'm tearing my hair out!

Re: changing file upload name

Posted: Tue Oct 20, 2009 3:51 pm
by John Cartwright
phpnoobyyy wrote:No joy...I'm tearing my hair out!
Post your updated code. When someone suggests somethiong and it doesn't work you should post exactly what you have tried so we are all on the same page.

Re: changing file upload name

Posted: Tue Oct 20, 2009 3:56 pm
by phpnoobyyy
apologies.

I changed this

Code: Select all

  1. $_FILES['[color=#FF0000]uploaded[/color]']['name']
   2. $_FILES['upload']['name']
to this

Code: Select all

  1. $_FILES['[color=#FF0000]upload[/color]']['name']
   2. $_FILES['upload']['name']

Re: changing file upload name

Posted: Wed Oct 21, 2009 2:27 pm
by phpnoobyyy
Anyone else have any insight?

Alternatively, could anyone suggest a viable alternative that i could try?

Am having no luck with this one at the moment.

Thanks

Re: changing file upload name

Posted: Wed Oct 21, 2009 3:22 pm
by John Cartwright

Code: Select all

$newname = rand(0, pow(10, 5)).date("M d, Y").$_POST['email'].$extension;
$basename = date("d.m.Y").$_POST['email'].$extension.( $_FILES['upload']['name']);
$base_file = ( $_FILES['upload']['name']);
Try removing the comma and spaces in the date function for $newname, i.e.,

Code: Select all

 
$newname = rand(0, pow(10, 5)).date("M-d-Y").$_POST['email'].$extension;
You are defining $basename and $base_file but they are never used.

Re: changing file upload name

Posted: Wed Oct 21, 2009 6:16 pm
by McInfo
1. Internet Explorer 7 uploads Word (.doc) files as "application/octet-stream".

2. Using unfiltered user input to define file names is not a good idea.

3. Use pathinfo($path, PATHINFO_EXTENSION) or substr(strrchr($path, '.'), 1) to get the file extension.

4. I would use the date format "Ymd" or "Y-m-d" or "YmdHis" for files so they will be sorted chronologically when sorted alphabetically and the date will always have a fixed length.

5. The $target string has no trailing slash and the string returned by basename() has no leading slash, so there is no slash between the $target and basename. That variable isn't used, so I guess it doesn't matter.

6. In addition to the two John mentioned, add to the list of garbage variables $target and $ok.

Edit: This post was recovered from search engine cache.