Upload PHP Script |
Welcome,
In this short tutorial I will show you how to create an upload php script. The documentation contains 4 simple steps which will teach you how to create the application.
Go and start with first step, then follow the other three!
CREATE TABLE `files` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `filename` VARCHAR( 255 ) NOT NULL , `timestamp` INT NOT NULL , `ip` VARCHAR( 50 ) NOT NULL ) ENGINE = MYISAM ;
$allowed = array("jpeg","gif","png","bmp");
if(isset($_POST['sb'])) { //check for valid extension $pathInfo = pathinfo($_FILES[upload][name]); $extension = $pathInfo['extension']; //choose directory/foolder to place the file in $dir = "uploads"; if(!in_array($extension, $allowed)) die("Extension not allowed!"); if(move_uploaded_file($_FILES['upload']['tmp_name'], "$dir/".$_FILES['upload']['name'])) { print "Your new file can be viewed/download at "; }else{ print "File could not be uploaded"; } }
$DatabaseHost = "localhost";
$DatabaseUsername = "root";
$DatabasePassword = "";
$DatabaseName = "upload_php_script";
$connection = mysql_connect($DatabaseHost, $DatabaseUsername, $DatabasePassword)
or die("Cannot connect to MySQL!");
mysql_select_db($DatabaseName, $connection) or die("Cannot find database!");
$sql = "insert into files values (null, '".htmlentities($_FILES['upload']['name'])."', 'now()', '$_SERVER[REMOTE_ADDR]')";
$rs = mysql_query($sql);
print mysql_error();