30111987
February 20th, 2006, 09:42 AM
hi folks,
I don't really know much about php, however I managed to bodge together a simple file upload script yesterday (i'm learning java at uni which seemed to help).
It uses an upload file (contains the form)
A config file (just so I can eaisly change the max upload size or path)
A download file (where the file and checked, renamed and the output made)
Seeing as anyone can upload a small image containing anything (Have to agree to terms but...) I would like to add in a way of logging the ip and the name of the output image. Just a simple text file for the log will suffice.
Any ideas?
Here is the download php source:
<?
include "content/updncfg.php";
//converts usefile size to kb
$divd = 1024;
$isize = $HTTP_POST_FILES['userfile']['size'] / $divd;
$isize2 = round($isize);
if (!isset($HTTP_POST_FILES['userfile'])) exit; //for security
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
echo "<h1>Error</h1>File is too big! <br /><br /> Your file is: $isize2 KB \n"; }
else{
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) {
if (($HTTP_POST_FILES['userfile']['type']=="image/jpeg")){
$type = ".jpg";
}
if (($HTTP_POST_FILES['userfile']['type']=="image/gif")){
$type = ".gif";
}
if (($HTTP_POST_FILES['userfile']['type']=="image/png")){
$type = ".png";
}
//generate random number
$number = rand(1,9999);
$fupl = "$number";
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "./".$path .$fupl.$type);
if (!$res) { echo "<h1>Error</h1>Didn't work, please try again.<br />\n"; } else {
//set url parts up
$domst = "http://";
$slash = "/";
$urlf = $domst .$domain .$slash .$path .$fupl .$type;
}
//if good so far the output the images and its url and filesize
echo "<h1>Upload Success</h1> \n";
echo "<div align='center'>\n";
echo "<img src='$urlf' alt='uploaded image' border='1' /> <br /> <br /> \n";
echo "The image URL for your phone is: <br /> \n";
echo "<strong> $urlf </strong><br />\n";
echo "File Size: $isize2 KB<br /> <br />\n";
echo "</div>\n";
}
//if not good then output the wrong filetype error
else { echo "<h1>Error</h1>You selected a wrong filetype! Needs to be .gif, .jpg or .png<br />\n"; }
}
}
?>
The whole script in action can be found here: http://www.free-mobile.net/index.php?id=upload
Thanks!
I don't really know much about php, however I managed to bodge together a simple file upload script yesterday (i'm learning java at uni which seemed to help).
It uses an upload file (contains the form)
A config file (just so I can eaisly change the max upload size or path)
A download file (where the file and checked, renamed and the output made)
Seeing as anyone can upload a small image containing anything (Have to agree to terms but...) I would like to add in a way of logging the ip and the name of the output image. Just a simple text file for the log will suffice.
Any ideas?
Here is the download php source:
<?
include "content/updncfg.php";
//converts usefile size to kb
$divd = 1024;
$isize = $HTTP_POST_FILES['userfile']['size'] / $divd;
$isize2 = round($isize);
if (!isset($HTTP_POST_FILES['userfile'])) exit; //for security
if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
echo "<h1>Error</h1>File is too big! <br /><br /> Your file is: $isize2 KB \n"; }
else{
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) {
if (($HTTP_POST_FILES['userfile']['type']=="image/jpeg")){
$type = ".jpg";
}
if (($HTTP_POST_FILES['userfile']['type']=="image/gif")){
$type = ".gif";
}
if (($HTTP_POST_FILES['userfile']['type']=="image/png")){
$type = ".png";
}
//generate random number
$number = rand(1,9999);
$fupl = "$number";
$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "./".$path .$fupl.$type);
if (!$res) { echo "<h1>Error</h1>Didn't work, please try again.<br />\n"; } else {
//set url parts up
$domst = "http://";
$slash = "/";
$urlf = $domst .$domain .$slash .$path .$fupl .$type;
}
//if good so far the output the images and its url and filesize
echo "<h1>Upload Success</h1> \n";
echo "<div align='center'>\n";
echo "<img src='$urlf' alt='uploaded image' border='1' /> <br /> <br /> \n";
echo "The image URL for your phone is: <br /> \n";
echo "<strong> $urlf </strong><br />\n";
echo "File Size: $isize2 KB<br /> <br />\n";
echo "</div>\n";
}
//if not good then output the wrong filetype error
else { echo "<h1>Error</h1>You selected a wrong filetype! Needs to be .gif, .jpg or .png<br />\n"; }
}
}
?>
The whole script in action can be found here: http://www.free-mobile.net/index.php?id=upload
Thanks!