PDA

View Full Version : php help - make a log file


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!

degsy
February 20th, 2006, 10:13 AM
You can use the file functions to write to the file.
It would probably be a good idea to have a delimiter such as a comma or tab.

http://uk.php.net/manual/en/function.fwrite.php


There are different ways to get the IP Address.
http://uk.php.net/manual/en/function.getenv.php

You could also use REMOTE_ADDR or HTTP_X_FORWARDED_FOR
Probably best to check which exist.


You may also want to use $_FILES instead of HTTP_POST_FILES
http://uk.php.net/reserved.variables

30111987
February 20th, 2006, 05:46 PM
great stuff!
thanks for your help!

I have added in the example code, modified it and now have a perfectly formatted log file showing the ip [tab] date a time [tab] filename [line break]

also changed to using $_FILES (though I am unsure what difference it will make other than reduce the overall script size)

degsy
February 20th, 2006, 07:09 PM
http://uk.php.net/features.file-upload

The global $_FILES exists as of PHP 4.1.0 (Use $HTTP_POST_FILES instead if using an earlier version). These arrays will contain all the uploaded file information.

30111987
February 20th, 2006, 07:16 PM
ok thanks