Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Magic Quotes And $_files
Jared
post May 6 2008, 05:56 AM
Post #1


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 40
Joined: 17-April 08
Member No.: 29,853



It recently came upon me that I was designing a website that used file uploads. Of course, PHP was my first choice to solve the problem. I'm pro-PHP and anti-ASP. Perl is somewhere in the middle. But anyway!

I have magic_quotes_runtime and magic_quotes_gpc both on and it seems to be causing some problems with file uploads.

If a user uploads a file called "Jared's Stuff.txt" (that was one of my tests) then magic_quotes insert a backslash before the apostrophe and $_FILES['file']['name'] becomes "'s Stuff.txt" since the backslash is interpreted as part of the path.

Is there any way I can fix this without disabling magic_quotes_gpc?

Thanks,
Jared
Go to the top of the page
 
+Quote Post
Mordent
post May 6 2008, 06:29 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 223
Joined: 30-June 07
Member No.: 23,045



I've a feeling you can use stripslashes() somehow, although how you make it act on the file is a little beyond me. I'll have to look in to it to see for myself, as I've never actually tried uploading anything with apostrophes in its name. If no one's looked at this by this evening I'll most likely have a tinker with PHP to see what I can come up with while patiently waiting for someone to look at my support ticket to get my hosting back up. wink.gif
Go to the top of the page
 
+Quote Post
Jared
post May 6 2008, 11:25 AM
Post #3


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 40
Joined: 17-April 08
Member No.: 29,853



I've thought about stripslashes (), but I don't think it's helpful in this case... stripping the slashes out of 's Stuff is still 's Stuff. And before my script is even processed all the $_FILES data is already set. So unfortunately it wouldn't be possible to strip the slashes out before the data is stored in the array. And also there is no set_magic_quotes_gpc () function to get rid of the magic quotes for the $_FILES array.

I am truly clueless.
Go to the top of the page
 
+Quote Post
TavoxPeru
post May 9 2008, 12:57 AM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 746
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



QUOTE(Jared @ May 6 2008, 06:25 AM) *
I've thought about stripslashes (), but I don't think it's helpful in this case... stripping the slashes out of 's Stuff is still 's Stuff. And before my script is even processed all the $_FILES data is already set. So unfortunately it wouldn't be possible to strip the slashes out before the data is stored in the array. And also there is no set_magic_quotes_gpc () function to get rid of the magic quotes for the $_FILES array.

I am truly clueless.

When i need to handle strings that must be escaped or not, I use a function that first tests if the Magic quotes is on with the get_magic_quotes_gpc() function, if it is true simply returns the string and if it is false it returns the string escaped with the mysql_real_escape_string() function.

CODE
<?php
function safeEscapeString($string)
{
    if (get_magic_quotes_gpc()) {
        return $string;
    }
    else {
        return mysql_real_escape_string($string);
    }
}
?>

This function works perfect if you need to insert or update your database data and as i just discover it does not work with uploaded files, so, to work with files you only need to add the stripslashes() function to the Magic quotes test.

CODE
<?php
function safeEscapeString1($string)
{
    if (get_magic_quotes_gpc()) {
        return stripslashes($string);
    }
    else {
        return mysql_real_escape_string($string);
    }
}
?>

I hope it helps you and BTW I test this code only with Internet Explorer 6 on a server running PHP 5.2.5.

Also the Magic Quotes feature has been removed from PHP 6.0.0:

QUOTE
Warning

This feature has been DEPRECATED and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

Best regards,
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Converting Flash Files To Gif(7)
  2. Inspirational Quotes(17)
  3. How To Hide Your Files In XP(18)
  4. Network Places: Alternative To Ftp On Windows(10)
  5. How To Transfer Files From One Computer To Another(16)
  6. How Do I Create And Write To Files?(4)
  7. VB.NET: Howto Add And Delete Files(8)
  8. Sharing Files In Windows Xp Home(15)
  9. Php : Variables Included Dont Work In Functions(4)
  10. How To Play *.rm Files With Media Player(12)
  11. How Do I Chmod Files On Astahost ?(20)
  12. Help Me: Need To Transfer Files From Old Computer(18)
  13. Switch Network Settings With Batch Files(18)
  14. Renaming Files (Using Excel Spreadsheet)(20)
  15. Get User Input From Vbscript For Batch Files(2)
  1. How To Embed Swf Files In Joomla ?(9)
  2. Creating Executable Jar Files(9)
  3. If You Have Some Private Files(17)
  4. Magic Nine?(4)
  5. Software To Concatenate Mp3 Files(8)
  6. Do You Believe In Magic?(42)
  7. Uploading More Than 30 Files In Less Than 10 Clicks?(5)
  8. Partition Magic Trouble When Shrinking An Ext2 Partition(14)
  9. Friends Can't Start The Exe Files I Send :((3)
  10. Creating Links With Quotes (")(2)
  11. Need Some Help/advice On Lost Files.(8)
  12. Need Some Help/advice On How To Restore Lost Files(5)
  13. Playing Two Wav Files Simultaneously In C#(4)


 



- Lo-Fi Version Time is now: 5th September 2008 - 11:20 PM