homepage Welcome to WebmasterWorld Guest from 54.166.173.147
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Upload problem
changing the name
WhosAWhata




msg:1279611
 1:00 am on Jun 12, 2004 (gmt 0)

i have a form with these input fields

<input type=text name=pagename>
<textarea name=pagedesc></textarea>
<input type=file name=uploadfile>

what I want to do is change the name of the file based on the $pagename and $pagedesc vars but i want to keep the file extention

this would be my rule

// the name will be exploded (later) at {*} for any confusion
$filename = date(mdy)."{*}".stripslashes($pagename)."{*}".stripslashes(ereg_replace("\n","<br>",$pagedesc))[/green].$extention

how would I
1. find the
$extention
2. put this into an upload script (temp folder at
tmp/ final at files/

i really appreciate any help

 

johnerazo




msg:1279612
 2:28 am on Jun 12, 2004 (gmt 0)

Use this to get the file extension part of the uploaded file. This applies to file extensions with 3 to 4 characters in length.

preg_match("([a-zA-Z\.]{3,4}$)", $_FILES['uploadfile']['name'], $ext);
$extension = $ext[0];

Then simply use move_uploaded_file() to move the file from tmp/ to final/

Hth,
John Erazo

WhosAWhata




msg:1279613
 3:20 am on Jun 12, 2004 (gmt 0)

a-zA-Z won't that apply only to files with only numerical names?

WhosAWhata




msg:1279614
 3:56 am on Jun 12, 2004 (gmt 0)

i have this code and it doesn't work,
it prints
upload failed
Array ( )

the empty array shows that no data is being passed in $_FILES

<?
if(!$uploadfile ¦¦!$pagename ¦¦!$pagedesc){
echo '<form action="'.$PHP_SELF.'" method="POST"><table><tr><td>Your Name:</td><td><input type="text" name="pagename"></td></tr>
<tr><td>Description of file:</td><td><textarea name="pagedesc" cols="100" rows="3"></textarea></td></tr>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<tr><td>File:</td><td><input type="file" name="uploadfile"></td></tr>
<tr><td colspan=2 align=center><input type=submit value=upload></td></tr></table></form>';
} else {
preg_match("([a-zA-Z\.]{3,4}$)", $_FILES['uploadfile']['name'], $ext);
$extension = $ext[0];
$filename = "files/".date(mdy)."{*}".stripslashes($pagename)."{*}".stripslashes(ereg_replace("\n","\\n",$pagedesc)).$extention;
if (!move_uploaded_file($_FILES['uploadfile']['tmp_name'],$filename)) {
echo "upload failed<br>";
print_r($_FILES);
exit;
die;
}
header("Location:index.php");
}
?>

johnerazo




msg:1279615
 4:20 am on Jun 12, 2004 (gmt 0)

Try this instead...

preg_match("(\.[_0-9a-zA-Z-]{3,4}$)", $_FILES['uploadfile']['name'], $ext);
$extention = $ext[0];

it will match file extension with alpha-numeric characters including underscore and dash, with 3 to 4 characters in length.

BTW, I have previously spelled mine with '$extension' while yours was '$extention'.

johnerazo




msg:1279616
 4:26 am on Jun 12, 2004 (gmt 0)

Your $filename will now contain the character '*' which is a wildcard and therefore renders the filename invalid.

WhosAWhata




msg:1279617
 4:45 am on Jun 13, 2004 (gmt 0)

i changed to

<?
if(!$uploadfile !$pagename !$pagedesc){
echo '<form action="'.$PHP_SELF.'" method="POST"><table><tr><td>Your Name:</td><td><input type="text" name="pagename"></td></tr>
<tr><td>Description of file:</td><td><textarea name="pagedesc" cols="100" rows="3"></textarea></td></tr>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<tr><td>File:</td><td><input type="file" name="uploadfile"></td></tr>
<tr><td colspan=2 align=center><input type=submit value=upload></td></tr></table></form>';
} else {
preg_match("([a-zA-Z\.]{3,4}$)", $_FILES['uploadfile']['name'], $ext);
$extention = $ext[0];
$filename = "files/".date(mdy)."
{~}".stripslashes($pagename)."{~}".stripslashes(ereg_replace("\n","\\n",$pagedesc)).$extention;
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'],$filename)) {
header("Location:index.php");
} else {
echo "upload failed<br>";
print_r($_FILES);
exit;
die;
}
}
?>

it still does the same thing, any ideas?

corz




msg:1279618
 7:17 pm on Jun 13, 2004 (gmt 0)


$file_ext = substr($file,strrpos($file,'.'));

;o)
(or

ps.. can someone please link to my "slightly improved upload script"!

johnerazo




msg:1279619
 1:47 am on Jun 14, 2004 (gmt 0)

You miss this part in the <form> tag.

enctype="multipart/form-data"

WhosAWhata




msg:1279620
 4:38 am on Jun 14, 2004 (gmt 0)

thanks guys I've almost got it I just have one problem left (i think)

this line (slightly modified since my last post)

$filename = "files/".date(mdy)."{~}".stripslashes(htmlspecialchars($pagename))."{~}".ereg_replace("\n","<br>",stripslashes(htmlspecialchars($pagedesc))).$extention;

only adds the <br> it doesn't get rid of the line break i think i need to add something about \r or something like that but i'm not sure how to add it...thanks for any help

johnerazo




msg:1279621
 4:58 am on Jun 14, 2004 (gmt 0)

You might be looking for the function nl2br(). Not sure though where to do you want to place it.

$filename = "files/".date(mdy)."{~}".stripslashes(htmlspecialchars($pagename))."{~}".stripslashes(nl2br(htmlspecialchars($pagedesc))).$extention;

WhosAWhata




msg:1279622
 5:08 am on Jun 14, 2004 (gmt 0)

fixed, i just switched \n to \r\n

new problem, reading my created files
i can't figure out how to properly encode the $file variable in index.php so that the browser will follow the link if i copy the url and paste it in the browser it works but clicking it doesn't

addtest.php
<?
if(!$uploadfile !$pagename !$pagedesc){
echo '<form action="'.$PHP_SELF.'" method="POST" enctype="multipart/form-data"><table><tr><td>Your Name:</td><td><input type="text" name="pagename"></td></tr>
<tr><td>Description of file:</td><td><textarea name="pagedesc" cols="100" rows="3"></textarea></td></tr>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<tr><td>File:</td><td><input type="file" name="uploadfile"></td></tr>
<tr><td colspan=2 align=center><input type=submit value=upload></td></tr></table></form>';
} else {
preg_match("([a-zA-Z\.]{3,4}$)", $_FILES['uploadfile']['name'], $ext);
$extention = $ext[0];
$filename = "files/".date(mdy)."{~}".stripslashes(htmlspecialchars($pagename))."{~}".ereg_replace("\r\n","<br>",stripslashes(htmlspecialchars($pagedesc))).$extention;
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'],$filename)) {
header("Location:index.php");
} else {
echo "upload failed<br>";
print_r($_FILES);
exit;
die;
}
}
?>

index.php
<?
$dir="files/";
$d=dir($dir);
$html="";
while($file = $d->read()){
if(($file!= ".")&&($file!= "..")){
$m = $file;
$m = explode("{~}",$m);
$date = $m[0];
$date = $date[0].$date[1]."/".$date[2].$date[3]."/".$date[4].$date[5].$date[6].$date[7];
$html .= "\n<tr><td>$date</td><td>".stripslashes($m[1])."</td><td>".stripslashes($m[2])."<br><<a href=\"files/".$file."\">Open</a>></td></tr>";
}
}
$d->close();
?><html><head>
<title>Message Board</title></head><body>
<h1><font color="green">Message Board</font></h1><br>
<table bgcolor="silver" width=75% align=left border=1>
<tr bgcolor=blue><td><h2><font color=yellow>Date:</font></h2></td><td><h2><font color=yellow>Posted By:</font></h2></td><td><h2><font color=yellow>Message:</font></h2></td></tr>
<?=$html?>
</table>

WhosAWhata




msg:1279623
 5:23 pm on Jun 14, 2004 (gmt 0)

the problem with nl2br is that it leaves the line break
example:

nl2br('hello
this text has
line breaks');

renders

hello<br />
this text has<br />
line breaks


$text = 'hello
this text has
line breaks';
ereg_replace("\r\n","<br>",$text);

renders

hello<br>this text has<br>line breaks


any ideas on my links anyone?
corz




msg:1279624
 6:03 pm on Jun 14, 2004 (gmt 0)
All browsers (are supposed to) produced DOS carriage returns "\r\n" in forms. I try never to forget that.

;o)
(or

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved