 |
Results from the most recent live auction are here.
29 members in the live chat room. Join Chat!
| |
08-13-2005, 12:07 PM
|
· #1 | | Senior Member Location: Belfast Join Date: Aug 2005
Posts: 1,196
NP$: 5.85 ( Donate)
| File Upload script - Need help. Ive got a file upload script, how can I make it so it gives a random name or ads a few digits on the end instead of saying "change the filename" all the time. Code: <?
//set these variables-----------------------------------------------------------------
$path = "upload/"; //path to your targetfolder after your domain
$max_size = 500000; //maximum filesize
//optionally
$domain = $_SERVER["HTTP_HOST"]; //your domainname - change if necessary like "www.wza.be"
//------------------------------------------------------------------------------------
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>file upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#990000" link="#990000" vlink="#990000" alink="#990000" leftmargin="20" topmargin="20" marginwidth="20" marginheight="20">
<FORM ENCTYPE="multipart/form-data" ACTION="index.php" METHOD="POST">
<strong><font color="#990000" face="Geneva, Arial, Helvetica, sans-serif">IMAGE (jpg/gif) </font></strong><font color="#990000">:</font>
<INPUT TYPE="file" NAME="userfile">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
<br>
<?
if (!isset($HTTP_POST_FILES['userfile'])) exit;
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">File is too big !</font><br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) {
if (file_exists("../".$path . $HTTP_POST_FILES['userfile']['name'])) {
echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">There already exists a file with this name, please rename your file and try again</font><br>\n"; exit; }
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "../".$path .$HTTP_POST_FILES['userfile']['name']);
if (!$res) { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Didn't work, please try again</font><br>\n"; exit; } else {
?>
<br>
<p>
<font color="#333333" face="Geneva, Arial, Helvetica, sans-serif">Find your file here: <strong><font color="#990000"><a href="http://<? echo $domain; ?>/<? echo "../".$path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>" target="_blank"><br>
http://<? echo $domain; ?>/<? echo $path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?><br>
</a></font></strong><br>
<?
}
echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\"><hr>";
echo "Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
echo "</font>";
echo "<br><br><img src=\"http://".$domain."/".$path.$HTTP_POST_FILES['userfile']['name']."\">";
} else { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Verkeerd bestandstype, enkel gif, jpg of png !!!</font><br>\n"; exit; }
}
?>
</body>
</html> |
| |
08-13-2005, 12:33 PM
|
· #2 | | Formally Mikor. Name: Michael Walker Location: East Yorkshire, England Join Date: Aug 2005
Posts: 2,539
NP$: 164.25 ( Donate)
| heres my script:
it generates a random number between 1 and 9999999999 so you can have a lot of files hosted. |
| |
08-13-2005, 01:25 PM
|
· #3 | | Senior Member Location: Belfast Join Date: Aug 2005
Posts: 1,196
NP$: 5.85 ( Donate)
| Were would I add that?
Thanks. |
| |
08-13-2005, 01:34 PM
|
· #4 | | Formally Mikor. Name: Michael Walker Location: East Yorkshire, England Join Date: Aug 2005
Posts: 2,539
NP$: 164.25 ( Donate)
| heres a script that I threw together:
The form on the page before: Code: <form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
<table width="414" border="0">
<tr>
<td width="158"><span class="style3">File Location: </span></td>
<td width="246"><input name="uploadedfile" type="file" id="uploadedfile"></td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="Upload File">
</p>
</form>
The php file (upload.php): PHP Code: <?php
// Where the file is going to be placed temporarly
$target_path = "/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$_FILES['uploadedfile']['tmp_name']; // temp file
$target_path = "./uploads/";
$oldfile = basename($_FILES['uploadedfile']['name']);
//Check the size
if($_FILES['uploadedfile']['size'] > 500000{
die( "File too large!");
}
//new file name
$newfile = Rand(1,9999999999) . $_FILES['uploadedfile']['name'];
// move the file to the final destination
$target_path = $target_path . basename($newfile);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
die( "File Uploaded");
} else{
die("Upload Error");
}
?>
That should work |
| |
08-14-2005, 07:51 PM
|
· #5 | | Eating Pie Name: Steve Location: Canada Join Date: Nov 2004
Posts: 2,282
NP$: 91.30 ( Donate)
| Another way to do this is. PHP Code: $uniq = substr( md5(uniqid (rand())), 0, 10 );
$ext = strtolower( substr($_FILES['uploadedfile']['name'], -3));
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $att_path."/".$uniq.".".$ext );
Edited your code: PHP Code: <?php
// Where the file is going to be placed temporarly
$_FILES['uploadedfile']['tmp_name']; // temp file
$target_path = "./uploads/";//Make sure chmodded to 777
$oldfile = basename($_FILES['uploadedfile']['name']);
$allowed_types = array("gif","jpg","png","jpeg","pdf","bmp");
//Check the size
if($_FILES['uploadedfile']['size'] > 500000{
die( "File too large!");
}
//Check file type
if(!in_array(stristr($_FILES['uploadedfile']['name'], "."), "$allowed_types")) {
die( "File type not allowed!");
}
//new file name
$uniq = substr( md5(uniqid (rand())), 0, 10 );
$ext = strtolower( substr($_FILES['uploadedfile']['name'], -3));
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path."/".$uniq.".".$ext );
?>
I add file type check code as well
Good Luck! |
| |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |