Previous Thread
Next Thread
Print Thread
Rate Thread
#268737 02/05/2004 10:20 AM
Joined: Sep 2002
Posts: 151
Member
Member
Offline
Joined: Sep 2002
Posts: 151
When moving to a new host I had ro rename lots of uploaded files that used letters that got messed up when transfering the files.

How do I prevent this in the future?

I would like to ad a rule that only allows upload if a file is named with anything from a-z. 0-9 and _.

I thougt this was already in threads (I run 6.4.1), but I can´t find any settings regarding this.

Anyone?

Sponsored Links
Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
I use something like this on the submitted filename :

//replace all spaces with underscore
$filename = str_replace(" ", "_", $filename );
//remove all non wanted characters
$filename = preg_replace('([^_a-zA-Z0-9\-\.])', '', $filename);

Joined: Sep 2002
Posts: 151
Member
Member
Offline
Joined: Sep 2002
Posts: 151
So with this users can name the files any way they want, and the name will be changed upon upload?

Where do you put this?

Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
>So with this users can name the files any way they want, and the name will be
>changed upon upload?

Yes.

>Where do you put this?

Well, I don´t know how exactly you handle uploads, but usually you fill have a form, something like this
<FORM ACTION="$PHP_SELF" METHOD=POST ENCTYPE="multipart/form-data">
<INPUT TYPE=FILE NAME="userfile"><BR>
<INPUT TYPE=HIDDEN NAME="action" VALUE="post">

So after the file is uploaded, the file is under the php variable $userfile .

To tidy its name, I do the following:

//tidy up user's filename
$userfile_name = str_replace(" ", "_", $userfile_name);
$userfile_name = preg_replace('([^_a-zA-Z0-9\-\.])', '', $userfile_name);
$userfile_name = str_replace(".jpeg", ".jpg", $userfile_name);
$userfile_name = str_replace(".JPG", ".jpg", $userfile_name);
$userfile_name = str_replace(".GIF", ".gif", $userfile_name);
$userfile_name = str_replace(".PNG", ".png", $userfile_name);

Read http://at2.php.net/features.file-upload

Here is a tutorial on file upload.
http://www.zend.com/zend/spotlight/uploading.php
also here:
http://forums.devshed.com/archive/5/2001/3/3/12353

Last edited by Anno; 02/06/2004 7:17 AM.
Joined: Sep 2002
Posts: 151
Member
Member
Offline
Joined: Sep 2002
Posts: 151
But will this really integrate with Threads?

Sponsored Links
Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
Ok....
So....backup the file you are going to edit.
In
addpost.php
find:
$FileName = "$Mnumber-{$HTTP_POST_FILES['userfile']['name']}";

Replace with:

$HTTP_POST_FILES['userfile']['name'] = str_replace(" ", "_", {$HTTP_POST_FILES['userfile']['name']});
$HTTP_POST_FILES['userfile']['name'] = preg_replace('([^_a-zA-Z0-9\-\.])', '', {$HTTP_POST_FILES['userfile']['name']});
$FileName = "$Mnumber-{$HTTP_POST_FILES['userfile']['name']}";

try it out and report if it works, I havent tested it becuase I don´t use this upload.

Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
If this doesn´t work (I´m not sure if one can asign new values to server side variables), then this should work:

In
addpost.php
find:
$FileName = "$Mnumber-{$HTTP_POST_FILES['userfile']['name']}";

Replace with:

$uploadname = $HTTP_POST_FILES['userfile']['name'];
$uploadname = str_replace(" ", "_", $uploadname);
$uploadname = preg_replace('([^_a-zA-Z0-9\-\.])', '', $uploadname);
$FileName = "$Mnumber-$uploadname";

Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
So did anyone try this out?

Joined: May 2001
Posts: 550
Code Monkey
Code Monkey
Offline
Joined: May 2001
Posts: 550
No? Ok.


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
badfrog
badfrog
somewhere on the coast of Maine
Posts: 94
Joined: March 2007
Forum Statistics
Forums63
Topics37,575
Posts293,930
Members13,823
Most Online6,139
Sep 21st, 2024
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2025 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.1
(Snapshot build 20240918)