changing file upload name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

changing file upload name

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: changing file upload name

Post 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.
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Re: changing file upload name

Post 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....?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: changing file upload name

Post by requinix »

Then you'll need to explain what
phpnoobyyy wrote:but for some reason fails to execute on IE, Safari, Opera
means.
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Re: changing file upload name

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: changing file upload name

Post by McInfo »

Code: Select all

$_FILES['uploaded']['name']
$_FILES['upload']['name']
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 12:59 pm, edited 1 time in total.
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Re: changing file upload name

Post by phpnoobyyy »

No joy...I'm tearing my hair out!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: changing file upload name

Post 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.
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Re: changing file upload name

Post 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']
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

Re: changing file upload name

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: changing file upload name

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: changing file upload name

Post 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.
Post Reply