Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
carinspector  
#1 Posted : Tuesday, December 19, 2006 4:59:56 AM(UTC)
carinspector

Rank: Member

Groups: Member
Joined: 12/19/2006(UTC)
Posts: 7

How do I remove the characters below from a filename......when a file with one of these characters is uploaded it locks the server down and I can't open, rename or delete the file....the only way to get the file off of the server is to delete the directory and re-create the directory again.
('!',"'",'\\','@','#','$','%','^','&','*','-','_',' ','|','}','{',']','[','/','?','`','~','+')




I tried this in the upload.php script, but it didn't work.

Code:
// Replace invalid name syntax.
         $replace = array('!',"'",'\\','@','#','$','%','^','&','*','-','_',' ','|','}','{',']','[','/','?','`','~','+');
         foreach($replace as $value){
            $imageName = str_replace("$value","",$imageName);	
         }
         if($imageName=="") $imageName = "noname" . rand(1,1000);


Any help is greatly appreciated!

Edited by user Tuesday, December 18, 2007 7:31:33 PM(UTC)  | Reason: Not specified

stark77  
#2 Posted : Tuesday, December 19, 2006 7:57:29 AM(UTC)
stark77

Rank: Member

Groups: Member
Joined: 10/29/2006(UTC)
Posts: 21

Quote:
carinspector (12/19/2006)
How do I remove the characters below from a filename......when a file with one of these characters is uploaded it locks the server down and I can't open, rename or delete the file....the only way to get the file off of the server is to delete the directory and re-create the directory again.
('!',"'",'\\','@','#','$','%','^','&','*','-','_',' ','|','}','{',']','[','/','?','`','~','+')




I tried this in the upload.php script, but it didn't work.

Code:
// Replace invalid name syntax.
         $replace = array('!',"'",'\\','@','#','$','%','^','&','*','-','_',' ','|','}','{',']','[','/','?','`','~','+');
         foreach($replace as $value){
            $imageName = str_replace("$value","",$imageName);	
         }
         if($imageName=="") $imageName = "noname" . rand(1,1000);


Any help is greatly appreciated!


It's probably easier to work from what you would accept, like numbers, letters, underscores and hyphens.

In which case, something like the following should do it

Code:
$imageName = preg_replace("/[^A-Za-z0-9-_]/", "", $imageName);

Edited by user Tuesday, December 18, 2007 7:32:06 PM(UTC)  | Reason: Not specified

carinspector  
#3 Posted : Tuesday, December 19, 2006 8:29:05 AM(UTC)
carinspector

Rank: Member

Groups: Member
Joined: 12/19/2006(UTC)
Posts: 7

I tried this and it still did not work.

File named john'spictures.jpg
When uploaded saves on the server as john\'spictures.jpg

Here is where I put it in the upload.php script:


Code:
$galleryPath = realpath("../Gallery");
//////////////////////////////////////
//Make file to hold descriptioons//
/////////////////////////////////////
if(file_exists($galleryPath . "/Descriptions.txt"))
{
	unlink($galleryPath . "/Descriptions.txt");
}

$fileCount = $_POST ["FileCount"];

$descrFile = fopen($galleryPath . "/Descriptions.txt", "wb");
/////////////////////////////////////////////
//Read Author name and images count//
////////////////////////////////////////////
$author = $_POST ['Author'];
fwrite($descrFile, $fileCount);
fwrite($descrFile, "\r\n");
fwrite($descrFile, $author);
fwrite($descrFile, "\r\n");

////////////////////////////
//process received files//
///////////////////////////	
for($i=1;$i<=$fileCount;$i++)
{
	$imageName = "SourceFile_" . $i;
         $imageName = preg_replace("A-Za-z0-9-_", "", $imageName);
	$size=$_FILES[$imageName][size]; 

              /////////////////////////////////////////////////////////////////////
	//Check whether file with such name already exists and if so,//  	
	//construct safe filename name (to avoid collision) and        //
            //add inspection number to filename                                 //
            /////////////////////////////////////////////////////////////////////

	$fileName = $user . "_" . $author . "_" . $_FILES[$imageName][name];
	$newFileName = $fileName;
	$J = 1;
	while (file_exists($galleryPath . "/" . $newFileName))
	{
		$newFileName = $J . "_" . $fileName;
		$J = $J + 1;
	}
	$fileName = $newFileName;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	
	if($size) 
	{ 
		copy($_FILES[$imageName][tmp_name], $galleryPath . "/" . $fileName);		
		$width = $_POST ['Width_' . $i];
		$height = $_POST ['Height_' . $i];
		$description = $_POST ['Description_' . $i];
		fwrite($descrFile, $fileName);
		fwrite($descrFile, "\r\n");
		fwrite($descrFile, $width);
		fwrite($descrFile, "\r\n");
		fwrite($descrFile, $height);
		fwrite($descrFile, "\r\n");
		fwrite($descrFile, $description);
		fwrite($descrFile, "\r\n");
	}
	$thumbnailName = "Thumbnail1_" . $i;
	$size=$_FILES[$thumbnailName][size];
	if($size) 
	{ 
		copy($_FILES[$thumbnailName][tmp_name], $galleryPath . "/Thumbnails/" . $fileName . ".jpg");
	}
}
fclose($descrFile);
?>

Edited by user Tuesday, December 18, 2007 7:34:18 PM(UTC)  | Reason: Not specified

Eugene Kosmin  
#4 Posted : Tuesday, December 19, 2006 12:32:01 PM(UTC)
Eugene Kosmin

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 9/19/2006(UTC)
Posts: 404

Was thanked: 12 time(s) in 12 post(s)
Hello,

You can store filenames in database and save files with the same indexed name (for example: image001.jpg, image002.jpg, etc).

Edited by user Monday, February 25, 2008 4:29:58 PM(UTC)  | Reason: Not specified

Best regards,
Eugene Kosmin.
Aurigma Development Team
carinspector  
#5 Posted : Tuesday, December 19, 2006 8:19:15 PM(UTC)
carinspector

Rank: Member

Groups: Member
Joined: 12/19/2006(UTC)
Posts: 7

Quote:
Eugene Kosmin (12/19/2006)
Hello,

You can store filenames in database and save files with the same indexed name (for example: image001.jpg, image002.jpg, etc).

Best regards,
Eugene Kosmin.


In a perfect world that works fine, but I have over 700 users and they all have their own way of naming files. When their filename has a special character or punctuation is when I am having the problem.....Since the users will not change the filenames (either out of ignorance, malice, or stupidity) I only have one choice....to change the filename as it is uplaoded to the server.

Again, all help and suggestions is greatly appreciated.
stark77  
#6 Posted : Thursday, December 21, 2006 5:51:33 AM(UTC)
stark77

Rank: Member

Groups: Member
Joined: 10/29/2006(UTC)
Posts: 21

Quote:

carinspector (12/19/2006)

Code:
////////////////////////////
//process received files//
///////////////////////////	
for($i=1;$i<=$fileCount;$i++)
{
	$imageName = "SourceFile_" . $i;
        $imageName = preg_replace("A-Za-z0-9-_", "", $imageName);
	$size=$_FILES[$imageName][size]; 




I think you are applying the regex to the wrong variable.

As you go through the loop, you will set $imageName to "SourceFile_1", "SourceFile_2", "SourceFile_3" etc. and then apply a regex to remove unwanted characters - but there won't be any because you already set $imageName yourself.

I'm assuming you want to actually apply the regex to the later variable $fileName

e.g.

Code:
$fileName = $user . "_" . $author . "_" . $_FILES[$imageName][name];


should be

Code:
$fileName = $user . "_" . $author . "_" . preg_replace("A-Za-z0-9-_", "", $_FILES[$imageName][name]);


As for the backslash in your example, that is probably caused by setting one of the php magic_quotes ini settings to automatically escape single quotes.


Edited by user Tuesday, December 18, 2007 7:35:15 PM(UTC)  | Reason: Not specified

carinspector  
#7 Posted : Friday, December 22, 2006 12:07:57 AM(UTC)
carinspector

Rank: Member

Groups: Member
Joined: 12/19/2006(UTC)
Posts: 7

Quote:
I think you are applying the regex to the wrong variable.

As you go through the loop, you will set $imageName to "SourceFile_1", "SourceFile_2", "SourceFile_3" etc. and then apply a regex to remove unwanted characters - but there won't be any because you already set $imageName yourself.

I'm assuming you want to actually apply the regex to the later variable $fileName

e.g.

Code:
$fileName = $user . "_" . $author . "_" . $_FILES[$imageName][name];


should be

Code:
$fileName = $user . "_" . $author . "_" . preg_replace("A-Za-z0-9-_", "", $_FILES[$imageName][name]);


As for the backslash in your example, that is probably caused by setting one of the php magic_quotes ini settings to automatically escape single quotes.




Ok..I finally got it! Since the script uses both $'s to name the file and it's description, I had to change $fileName and $imageName. Here is the script as it is now. BTW Thanks to all of you...and Merry Christmas!


Code:
$galleryPath = realpath("../Gallery");
//////////////////////////////////////
//Make file to hold descriptioons//
/////////////////////////////////////
if(file_exists($galleryPath . "/Descriptions.txt"))
{
unlink($galleryPath . "/Descriptions.txt");
}

$fileCount = $_POST ["FileCount"];

$descrFile = fopen($galleryPath . "/Descriptions.txt", "wb");
/////////////////////////////////////////////
//Read Author name and images count//
////////////////////////////////////////////
$author = $_POST ['Author'];
fwrite($descrFile, $fileCount);
fwrite($descrFile, "\r\n");
fwrite($descrFile, $author);
fwrite($descrFile, "\r\n");

////////////////////////////
//process received files//
/////////////////////////// 
for($i=1;$i<=$fileCount;$i++)
{
$imageName = "SourceFile_" . $i;
$size=$_FILES[$imageName][size]; 
$imageName = preg_replace("/[^A-Za-z0-9-_.]/", "", $imageName);
/////////////////////////////////////////////////////////////////////
//Check whether file with such name already exists and if so,// 
//construct safe filename name (to avoid collision) and //
//add inspection number to filename //
/////////////////////////////////////////////////////////////////////

$fileName = $user . "_" . $author . "_" . $_FILES[$imageName][name];
$newFileName = $fileName;
$J = 1;
while (file_exists($galleryPath . "/" . $newFileName))
{
$newFileName = $J . "_" . $fileName;
$J = $J + 1;
}
$fileName = $newFileName;

[highlight=#ffff11]$fileName = preg_replace("/[^A-Za-z0-9-_.]/", "", $fileName);[/highlight]

////////////////////////////////////////////////////////////////////////////////////////////////////////////////


if($size) 
{ 
copy($_FILES[$imageName][tmp_name], $galleryPath . "/" . $fileName); 
$width = $_POST ['Width_' . $i];
$height = $_POST ['Height_' . $i];
$description = $_POST ['Description_' . $i];
fwrite($descrFile, $fileName);
fwrite($descrFile, "\r\n");
fwrite($descrFile, $width);
fwrite($descrFile, "\r\n");
fwrite($descrFile, $height);
fwrite($descrFile, "\r\n");
fwrite($descrFile, $description);
fwrite($descrFile, "\r\n");
}
$thumbnailName = "Thumbnail1_" . $i;
$size=$_FILES[$thumbnailName][size];
if($size) 
{ 
copy($_FILES[$thumbnailName][tmp_name], $galleryPath . "/Thumbnails/" . $fileName . ".jpg");
}
}
fclose($descrFile);
?> 

Edited by user Tuesday, December 18, 2007 7:36:34 PM(UTC)  | Reason: Not specified

Alex Makhov  
#8 Posted : Friday, December 22, 2006 2:57:12 PM(UTC)
Alex Makhov

Rank: Advanced Member

Groups: Member
Joined: 8/3/2003(UTC)
Posts: 998

Hello,

We wish all our customers a Merry Christmas and a Happy New Year too!
Sincerely yours,
Alex Makhov

UserPostedImage Follow Aurigma on Twitter!
stark77  
#9 Posted : Friday, December 22, 2006 8:52:20 PM(UTC)
stark77

Rank: Member

Groups: Member
Joined: 10/29/2006(UTC)
Posts: 21

Quote:
Ok..I finally got it! Since the script uses both $'s to name the file and it's description, I had to change $fileName and $imageName. Here is the script as it is now. BTW Thanks to all of you...and Merry Christmas!


You don't need the first preg_replace as you will never have unacceptable characters in it - as I mentioned in an earlier post, it's just on the second parameter that you need it.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.